For Developers
How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in C++ — plus why chrono, not time_t, is the version you actually want.
#include <chrono>
auto now = std::chrono::system_clock::now();
auto ts = std::chrono::duration_cast<std::chrono::seconds>(
now.time_since_epoch()).count();
std::time_t t = 1750000000;
std::tm* utc = std::gmtime(&t);
// utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday ...
std::tm t{};
t.tm_year = 2025 - 1900; t.tm_mon = 5; t.tm_mday = 15;
t.tm_hour = 12; t.tm_min = 0; t.tm_sec = 0;
std::time_t ts = timegm(&t); // GMT, not local (mktime uses local time)
The standard library gives you two ways to do this, and they behave differently. mktime() interprets a tm struct as local time using the system's timezone, which makes the same code produce a different timestamp depending on which machine or container it runs on. timegm() (POSIX/glibc, or _mkgmtime on Windows) treats it as UTC and is almost always what a server or cross-platform build actually wants. If you're on C++20, std::chrono's calendar and timezone types (year_month_day, zoned_time) finally make this explicit instead of implicit, and are worth the migration if your toolchain supports them.
Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.
Related
Every language's snippets side by side, for quick comparison.
Convert any timestamp instantly, no code required.
The same conversions with Rust's std::time and chrono crate.