Call std::chrono::system_clock::now() and format it with C++20's std::format: std::format("{:%Y-%m-%d %H:%M:%S}", std::chrono::floor<std::chrono::seconds>(now)) prints 2026-11-19 18:24:11, in UTC. For local time, wrap it in std::chrono::zoned_time{std::chrono::current_zone(), now}. To time code, take two std::chrono::steady_clock::now() readings and subtract them.
C++ gives you two toolkits for this. <chrono> has clocks and durations since C++11, and C++20 added dates, time zones and std::format support, which together answer the question without any C functions. The older <ctime> route (std::time, std::localtime, strftime) still works and is in a lot of code, with a thread-safety trap worth knowing. The sections below cover formatting the current time, the date as numbers, local time and time zones, the C functions, Unix timestamps and measuring elapsed time, then a table to pick from. The machine behind this page runs in UTC, so its local time and UTC are the same. Each example runs on this page: hit Run, then edit the code and run it again.
1system_clock::now() and std::format (C++20)Recommended
std::chrono::system_clock::now() returns a time_point: the current moment as a count of ticks since 1970-01-01 UTC. In C++20 it prints with << and formats with std::format, which takes the same % codes as strftime: %Y year, %m month, %d day, %H:%M:%S time, %F for the whole date and %T for the whole time. Two things to know: the result is UTC, and %S prints as many decimals as the time point holds, so round it with std::chrono::floor to the precision you want first.
#include <chrono> #include <format> #include <iostream> int main() { // The current moment on the wall clock. system_clock counts UTC. auto now = std::chrono::system_clock::now(); // Printed as it is, the time point keeps its nanoseconds std::cout << "raw: " << now << '\n'; // Round down to whole seconds first, or %S prints the fraction too auto secs = std::chrono::floor<std::chrono::seconds>(now); std::cout << "custom: " << std::format("{:%Y-%m-%d %H:%M:%S}", secs) << '\n'; // Milliseconds: floor to milliseconds and %T shows three decimals auto ms = std::chrono::floor<std::chrono::milliseconds>(now); std::cout << "millis: " << std::format("{:%F %T}", ms) << '\n'; // %F is %Y-%m-%d and %T is %H:%M:%S; Z marks the time as UTC std::cout << "iso: " << std::format("{:%FT%TZ}", secs) << '\n'; std::cout << "date: " << std::format("{:%F}", secs) << '\n'; std::cout << "time: " << std::format("{:%H:%M}", secs) << '\n'; std::cout << "words: " << std::format("{:%A, %d %B %Y}", secs) << '\n'; }
Output
One run printed raw: 2026-11-19 18:24:11.949107128: the clock ticks in nanoseconds here, and printing the time point shows all nine digits. The floored versions gave 2026-11-19 18:24:11, 2026-11-19 18:24:11.949 with milliseconds, the ISO 8601 form 2026-11-19T18:24:11Z and Thursday, 19 November 2026. Without the floor, "{:%Y-%m-%d %H:%M:%S}" prints 2026-11-19 18:23:20.474190957, fraction included. Your output shows the moment you press Run. All of it is UTC whatever the machine's time zone, because a system_clock time point carries no zone; section 3 converts it. More format options are in formatting a string.
2Year, month, day and time of day as numbers
To get the current year or day as an int, split the time point with the C++20 calendar types. floor<days> cuts it to midnight; a year_month_day built from that holds the date, and the rest of the day goes into an hh_mm_ss. Each part converts to a number with a cast or count(). The same types do date arithmetic, including the cases that need care, such as adding a year to 29 February.
#include <chrono> #include <iostream> // Split a time point into calendar date and time of day void describe(std::chrono::sys_seconds t) { auto day_start = std::chrono::floor<std::chrono::days>(t); std::chrono::year_month_day date{day_start}; std::chrono::hh_mm_ss time{t - day_start}; std::chrono::weekday wd{day_start}; // Each part converts to a plain number with a cast or count() int year = static_cast<int>(date.year()); unsigned month = static_cast<unsigned>(date.month()); unsigned day = static_cast<unsigned>(date.day()); long hour = time.hours().count(); long minute = time.minutes().count(); std::cout << "year " << year << ", month " << month << ", day " << day << ", hour " << hour << ", minute " << minute << ", " << wd << '\n'; } int main() { using namespace std::chrono; // Today, from the clock (UTC) describe(floor<seconds>(system_clock::now())); // A fixed moment built from calendar literals: 29 Feb 2024, 23:59:30 describe(sys_days{2024y / February / 29} + 23h + 59min + 30s); // Date arithmetic: add days through sys_days year_month_day leap{2024y / February / 29}; std::cout << "next day: " << year_month_day{sys_days{leap} + days{1}} << '\n'; // Adding years or months keeps the day, which may not exist year_month_day next_year = leap + years{1}; std::cout << "plus a year: " << next_year << '\n'; if (!next_year.ok()) { next_year = next_year.year() / next_year.month() / last; // clamp } std::cout << "clamped: " << next_year << '\n'; // Subtracting two dates gives a count of days days gap = sys_days{2025y / 1 / 1} - sys_days{leap}; std::cout << "days until 2025-01-01: " << gap.count() << '\n'; }
Output
One run printed year 2026, month 11, day 19, hour 18, minute 30, Thu for the current moment, and the fixed moment always gives year 2024, month 2, day 29, hour 23, minute 59, Thu. Adding a day goes through sys_days and rolls over to 2024-03-01. Adding years{1} only changes the year field, so the result prints 2025-02-29 is not a valid date; check ok() and clamp with last to get 2025-02-28, or convert to sys_days to roll over to March instead. Subtracting two dates counts the days between them: 307. These parts are UTC; take them from a zoned_time's get_local_time() to get the local date, which differs near midnight.
3Local time and time zones: zoned_time
A std::chrono::zoned_time pairs a UTC time point with a time zone, and formats as the local time there. current_zone() is the machine's own zone; any IANA name such as "Europe/Paris" works too, looked up in the time zone database the standard library ships with or reads from the system. Daylight saving time is handled for you: %Z prints the abbreviation in force at that moment and %z the offset from UTC.
#include <chrono> #include <format> #include <iostream> #include <stdexcept> int main() { using namespace std::chrono; // Local time now: pair the UTC time point with the machine's zone auto now = floor<seconds>(system_clock::now()); zoned_time local{current_zone(), now}; std::cout << "this machine: " << current_zone()->name() << '\n'; std::cout << "local now: " << std::format("{:%F %T %Z}", local) << '\n'; // Any IANA zone by name; the same instant shown in three places sys_seconds launch = sys_days{2026y / July / 4} + 16h; for (const char* name : {"America/New_York", "Europe/Paris", "Asia/Kolkata"}) { zoned_time zt{name, launch}; std::cout << std::format("{:<17}{:%F %H:%M %Z (%z)}", name, zt) << '\n'; } // Same zone in January: no daylight saving, so a different offset sys_seconds january = sys_days{2026y / January / 4} + 16h; zoned_time winter{"America/New_York", january}; std::cout << std::format("{:<17}{:%F %H:%M %Z (%z)}", " in January", winter) << '\n'; // An unknown zone name throws std::runtime_error try { zoned_time bad{"Mars/Olympus_Mons", launch}; } catch (const std::runtime_error& e) { std::cout << "error: " << e.what() << '\n'; } }
Output
This machine reports Etc/UTC, so local time here is UTC: one run printed local now: 2026-11-19 18:30:15 UTC. On your own computer the same line shows your zone. The fixed instant, 16:00 UTC on 4 July, comes out as 12:00 EDT (-0400) in New York, 18:00 CEST (+0200) in Paris and 21:30 IST (+0530) in Kolkata, and the same hour in January is 11:00 EST (-0500) because New York is off daylight saving. A name the database does not know throws std::runtime_error, here std::chrono::tzdb: cannot locate zone: Mars/Olympus_Mons, so catch it when the zone comes from user input. Before C++20, or on a standard library without time zone support, Howard Hinnant's date library offers the same API, since it is where this part of the standard came from.
4The C way: time, localtime and strftime
Before C++20 the usual answer was the C library: std::time(nullptr) for the current time as a std::time_t (seconds since 1970), std::localtime or std::gmtime to break it into a std::tm with local or UTC fields, and std::strftime or std::put_time to format it. It still compiles everywhere, but the std::tm fields have odd offsets and the two conversion functions share one static result, which the last lines show.
#include <ctime> #include <iomanip> #include <iostream> int main() { // Seconds since 1970-01-01 UTC, the C way std::time_t now = std::time(nullptr); // localtime and gmtime split it into a std::tm (local zone vs UTC) std::tm local = *std::localtime(&now); // copy the result right away std::cout << "put_time: " << std::put_time(&local, "%Y-%m-%d %H:%M:%S %Z") << '\n'; // strftime writes into a char buffer and returns the length char buf[64]; std::size_t n = std::strftime(buf, sizeof buf, "%d/%m/%Y %I:%M %p", &local); std::cout << "strftime: " << buf << " (" << n << " chars)\n"; // ctime: fixed format, and it adds its own '\n' std::cout << "ctime: " << std::ctime(&now); // The std::tm fields: years count from 1900, months from 0 std::time_t t = 1700000000; std::tm utc = *std::gmtime(&t); std::cout << "tm_year=" << utc.tm_year << " tm_mon=" << utc.tm_mon << " tm_mday=" << utc.tm_mday << '\n'; std::cout << "so the date is " << utc.tm_year + 1900 << '-' << utc.tm_mon + 1 << '-' << utc.tm_mday << '\n'; // Trap: localtime and gmtime return a pointer to one shared std::tm, // so the next call overwrites what the first pointer shows std::time_t t2 = 0; std::tm* a = std::gmtime(&t); std::tm* b = std::gmtime(&t2); std::cout << "same object: " << std::boolalpha << (a == b) << ", a now says " << a->tm_year + 1900 << '\n'; }
Output
One run printed put_time: 2026-11-19 18:29:55 UTC, strftime: 19/11/2026 06:29 PM (19 chars) and ctime: Thu Nov 19 18:29:55 2026. The fixed timestamp shows the field trap: tm_year=123 tm_mon=10 tm_mday=14 means 2023-11-14, because tm_year counts from 1900 and tm_mon from 0. The last line is the reason to copy the result at once: same object: true, a now says 1970, the second gmtime call overwrote what the first pointer showed. The same shared object makes localtime unsafe in threads. Use localtime_r(&t, &out) on Linux and macOS or localtime_s(&out, &t) on Windows, which write into your own std::tm (MSVC warns about localtime for this reason), or use zoned_time from section 3. strftime returns 0 when the buffer is too small.
5A Unix timestamp in seconds or milliseconds
For a number to store or send, take time_since_epoch() of the current time point and duration_cast it to the unit you want; count() gives the integer. C++20 guarantees that system_clock counts from the Unix epoch, 1970-01-01 00:00:00 UTC, which every major library already did. Going the other way, build a sys_seconds from the number, or use system_clock::from_time_t for a std::time_t.
#include <chrono> #include <cstdint> #include <ctime> #include <iostream> int main() { using namespace std::chrono; // Since C++20 system_clock's epoch is 1970-01-01 UTC: Unix time. // duration_cast picks the unit; it truncates toward zero. auto since_epoch = system_clock::now().time_since_epoch(); std::int64_t s = duration_cast<seconds>(since_epoch).count(); std::int64_t ms = duration_cast<milliseconds>(since_epoch).count(); std::int64_t us = duration_cast<microseconds>(since_epoch).count(); std::cout << "seconds: " << s << '\n'; std::cout << "milliseconds: " << ms << '\n'; std::cout << "microseconds: " << us << '\n'; // The C call gives the same seconds as a std::time_t std::cout << "time(nullptr) agrees: " << std::boolalpha << (std::time(nullptr) - s <= 1) << '\n'; // Back from a Unix timestamp to a date sys_seconds t{seconds{1700000000}}; std::cout << "1700000000 is " << t << " UTC\n"; sys_time<milliseconds> t_ms{milliseconds{1700000000123}}; std::cout << "1700000000123 ms is " << t_ms << " UTC\n"; // And from a std::time_t to a time point std::time_t old = 86400; std::cout << "time_t 86400 is " << system_clock::from_time_t(old) << '\n'; }
Output
One run printed seconds: 1795112761 and milliseconds: 1795112761122, the same unit as JavaScript's Date.now() gives, and std::time(nullptr) agreed with the seconds. Store the counts in a std::int64_t: milliseconds overflow a 32-bit int long ago. Converting back, 1700000000 is 2023-11-14 22:13:20 UTC and the millisecond value keeps its fraction, 22:13:20.123. from_time_t returns a time point in the clock's own nanosecond units, hence 1970-01-02 00:00:00.000000000; floor<seconds> it before printing if the zeros are unwanted.
6Measuring elapsed time with steady_clock
To time a piece of code, read std::chrono::steady_clock::now() before and after and subtract: the difference is a duration. Use steady_clock, not system_clock: the wall clock can be moved by NTP or by hand while you measure, which gives wrong or even negative intervals, and the steady clock never goes backwards. Convert the result with duration_cast for whole units, or to a duration<double> to keep the fraction.
#include <chrono> #include <cstdint> #include <iostream> #include <thread> // Some work to time std::uint64_t busy_sum(std::uint64_t n) { std::uint64_t total = 0; for (std::uint64_t i = 0; i < n; ++i) { total += i % 7; } return total; } int main() { using namespace std::chrono; // steady_clock never jumps (NTP, manual clock changes), so it is the // clock for intervals. system_clock is for "what time is it". auto start = steady_clock::now(); std::this_thread::sleep_for(150ms); auto elapsed = steady_clock::now() - start; // duration_cast to whole milliseconds (truncates) auto ms = duration_cast<milliseconds>(elapsed); std::cout << std::boolalpha; std::cout << "slept at least 150 ms: " << (ms >= 150ms) << '\n'; // A floating-point duration keeps the fraction: seconds as a double duration<double> secs = elapsed; std::cout << "under a second: " << (secs.count() < 1.0) << '\n'; // Timing real work; C++20 prints a duration with its unit start = steady_clock::now(); std::uint64_t total = busy_sum(10'000'000); auto took = steady_clock::now() - start; std::cout << "sum=" << total << " took " << duration_cast<milliseconds>(took) << '\n'; duration<double, std::milli> took_ms = took; std::cout << "as a double: " << took_ms.count() << " ms\n"; // high_resolution_clock is only an alias, and here not a steady one std::cout << "high_resolution_clock is steady: " << high_resolution_clock::is_steady << '\n'; }
Output
The checks print slept at least 150 ms: true and under a second: true on every run; the timing of the loop varies, and one run printed sum=29999994 took 29ms and as a double: 29.5319 ms. C++20 prints a duration with its unit, so 29ms needs no count(). duration_cast truncates, which is why the double is the one to report for short intervals. high_resolution_clock is only an alias for one of the other two clocks; here it is system_clock and prints high_resolution_clock is steady: false, so it is not the safe choice for timing. For real benchmarks, repeat the measurement and use the result of the work, or an optimizing compiler may remove it. The pause itself is covered in sleeping for milliseconds.
7Which should you use?
| Method | Gives | Standard | Best for |
|---|---|---|---|
| system_clock::now() + std::format | UTC date and time | C++20 | Printing or logging the current time |
| year_month_day / hh_mm_ss | Parts as numbers | C++20 | Current year, month, day; date arithmetic |
| zoned_time + current_zone() | Local time, any zone | C++20 | Local time, other time zones, daylight saving |
| time + localtime + strftime | Local or UTC, shared buffer | C | Compilers before C++20; copy the std::tm at once |
| time_since_epoch + duration_cast | Integer timestamp | C++11 | Unix time in seconds or milliseconds |
| steady_clock::now() difference | Duration, never jumps | C++11 | Measuring elapsed time |
Frequently asked questions
How do I get the current time in milliseconds in C++?
As a number since 1970, cast the time since the epoch: std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(), which returned 1795112761122 in one run; keep it in a std::int64_t. As text, floor the time point to milliseconds and format it: std::format("{:%F %T}", std::chrono::floor<std::chrono::milliseconds>(now)) printed 2026-11-19 18:24:11.949.
Why does C++ print UTC instead of my local time?
system_clock::now() is a UTC time point with no zone attached, and std::format or << on it always prints UTC. For local time, format a std::chrono::zoned_time{std::chrono::current_zone(), now} instead, or in pre-C++20 code pass the std::time_t to std::localtime. The machine behind this page is set to Etc/UTC, so both give the same result here.
Is std::localtime thread-safe?
No. std::localtime and std::gmtime return a pointer to one static std::tm that the next call overwrites, from any thread; this page shows two gmtime calls returning the same object. Copy the result immediately in single-threaded code, and in threaded code use localtime_r(&t, &out) on POSIX or localtime_s(&out, &t) on Windows (note the reversed arguments), which fill your own std::tm. That is also why MSVC warns that localtime may be unsafe. In C++20, zoned_time avoids the issue.
Should I use high_resolution_clock to measure time?
No. The standard only says it is the clock with the shortest tick, and in practice it is an alias for system_clock or steady_clock. With GCC's library it is system_clock, so high_resolution_clock::is_steady is false and it can jump when the system time changes. Use std::chrono::steady_clock for intervals; it has nanosecond ticks on mainstream platforms anyway.
How do I get the current year as an int in C++?
In C++20: std::chrono::year_month_day ymd{std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now())}; then int year = static_cast<int>(ymd.year());, and static_cast<unsigned> for the month and day. That is the UTC date. With <ctime>, add 1900 to tm_year from std::localtime, and 1 to tm_mon, which counts months from 0.
Why does %S print fractional seconds in std::format?
With a time point, %S prints the seconds with as many decimals as its type holds, and system_clock::now() is in nanoseconds here, so "{:%Y-%m-%d %H:%M:%S}" printed 2026-11-19 18:23:20.474190957. Round first with std::chrono::floor<std::chrono::seconds>(now) to get 18:23:20, or floor to milliseconds for three decimals.