HOWTO · C++
How to Get Time in Milliseconds in C++
This article introduces how to get time in milliseconds in C++
On this page
To get the current wall-clock timestamp in milliseconds in C++, capture std::chrono::system_clock::now() once and convert the duration since that clock’s epoch. Use this for a timestamp that can be compared with other wall-clock records. For measuring how long an operation takes, use a monotonic clock such as std::chrono::steady_clock instead; a system clock can be adjusted while the program runs.
Choose the clock based on whether you need a timestamp or elapsed time
A timestamp represents a point on the system’s wall clock. An elapsed-time measurement is the difference between two clock readings. std::chrono::system_clock is intended for wall-clock time and is the standard C++ choice for a current timestamp. Its epoch is clock-defined, so portable C++ code should not assume that its raw duration always uses the Unix epoch. If an application needs a Unix timestamp, verify that assumption for its target platform or use an API that specifies Unix time.
For elapsed time, take both readings from std::chrono::steady_clock and subtract them. That clock is intended to advance steadily and is not a calendar timestamp. This distinction prevents using a clock adjustment as part of a duration measurement.
Get a millisecond timestamp with std::chrono::system_clock
Call now() once, then use time_since_epoch() and duration_cast to express that time point’s duration in milliseconds. Casting a finer duration to milliseconds truncates the fractional remainder; it does not round to the nearest millisecond. The clock’s actual resolution may also be coarser than a millisecond, so the unit of the returned integer does not promise millisecond accuracy.
For a current wall-clock timestamp in milliseconds, capture one time point and convert its duration:
#include <chrono>
#include <cstdlib>
#include <iostream>
int main() {
const auto now = std::chrono::system_clock::now();
const auto milliseconds =
std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()).count();
std::cout << milliseconds << '\n';
return EXIT_SUCCESS;
}
This program prints a timestamp value that changes with the clock and the time it runs, so there is no fixed number to copy as its output.
Get a POSIX timestamp with gettimeofday
On POSIX systems, gettimeofday is an alternative that reports seconds and microseconds since the Unix epoch in a timeval structure. Include <sys/time.h> and check its return value before reading the fields. Convert microseconds to milliseconds by dividing tv_usec by 1,000, then combine that value with the seconds. This API is POSIX-specific and is not a portable C++ interface for every operating system.
#include <cstdlib>
#include <iostream>
#include <sys/time.h>
int main() {
timeval now{};
if (gettimeofday(&now, nullptr) != 0) {
std::cout << "gettimeofday failed\n";
return EXIT_FAILURE;
}
const long long milliseconds =
static_cast<long long>(now.tv_sec) * 1000 + now.tv_usec / 1000;
std::cout << milliseconds << '\n';
return EXIT_SUCCESS;
}
The printed value varies with the clock. Use this approach when POSIX compatibility is specifically needed; otherwise, prefer the standard-library std::chrono::system_clock example.
Understand why time() cannot provide millisecond precision
The C library function time() returns a time_t value in whole seconds. Multiplying it by 1,000 changes the scale of the number but cannot recover the fractional part of a second. Its result is therefore only a millisecond-scaled representation of a whole-second timestamp, not a measurement with millisecond precision.
#include <ctime>
#include <cstdlib>
#include <iostream>
int main() {
const std::time_t seconds = std::time(nullptr);
const long long milliseconds = static_cast<long long>(seconds) * 1000;
std::cout << milliseconds << '\n';
return EXIT_SUCCESS;
}
This value changes over time but advances in whole-second steps. Choose time() only when second-level resolution is sufficient. For ordinary C++ timestamp code, use std::chrono::system_clock; use gettimeofday when a POSIX-specific interface is required, and use a monotonic clock for elapsed-time measurements.