This post will discuss how to get the current timestamp in milliseconds since Epoch in C++.

1. Using std::chrono

Since C++11, we can use std::chrono to get elapsed time since Epoch. The idea is to get the current system time with std::chrono::system_clock::now(). Then invoke the time_since_epoch() function to get the duration representing the amount of time elapsed since Epoch.

The following code example demonstrates its usage. It translates the duration to milliseconds and seconds using std::chrono::duration_cast. Note that C++20 standardized the time_since_epoch() function to Unix Epoch.

Download  Run Code

2. Using std::time

The std::time returns the current calendar time encoded as a std::time_t object. It is defined in the header <ctime>. The following code example demonstrates its usage to get the number of seconds that have passed since the epoch.

Download  Run Code

That’s all about getting the current timestamp in milliseconds since Epoch in C++.