How do I get the current Unix timestamp in C++?
Use std::chrono::system_clock::now(), then convert to seconds with time_point_cast or duration_cast. This is the most portable modern C++ approach.
If you searched for C++ Unix timestamp, you usually need one of two things: a stable 10-digit epoch value for APIs, or a 13-digit millisecond value for logs and event pipelines. In 2026, the recommended approach is to use std::chrono from the standard library instead of manual arithmetic. The chrono API keeps units explicit, which prevents common seconds-vs-milliseconds mistakes when C++ services exchange data with JavaScript and database layers.
A Unix timestamp is always UTC-based. That means you should store raw integers in your backend and convert to local time only when rendering user-facing output. This avoids timezone drift and daylight-saving bugs in distributed systems. If you still maintain legacy code that uses time_t, keep it for compatibility but normalize new modules around chrono so your time math stays consistent.
Current Unix timestamp (seconds)
using namespace std::chrono; auto now = system_clock::now(); auto sec = duration_cast<seconds>(now.time_since_epoch()).count();
Current Unix timestamp (milliseconds)
using namespace std::chrono; auto now = system_clock::now(); auto ms = duration_cast<milliseconds>(now.time_since_epoch()).count();
Convert epoch seconds to UTC date
std::time_t ts = 1700000000; std::tm* utc = std::gmtime(&ts); std::cout << std::put_time(utc, "%Y-%m-%d %H:%M:%S UTC");
Need C-specific patterns? Visit Unix timestamp in C. If your services are mixed-language, compare with Go Unix timestamp examples and validate any historical log value using Unix timestamp to date.
For live checks, open current timestamp now and compare it against your C++ output.
Testing timestamp patterns in logs or payload text? Use Regex Tester & Debugger to validate format rules before deploying.
Use std::chrono::system_clock::now(), then convert to seconds with time_point_cast or duration_cast. This is the most portable modern C++ approach.
Use std::chrono for new code because it gives explicit units and safer conversions. Keep time_t for interoperability with older C APIs or legacy systems.
Use duration_cast<std::chrono::milliseconds>(system_clock::now().time_since_epoch()).count() to get a 13-digit millisecond value.
Create a std::time_t from the epoch seconds and format with std::gmtime plus std::put_time for UTC output, or std::localtime for local display.