How do I format time points with format in C++?

Formatting time points in C++ can be done using the std::chrono library combined with the std::put_time function for output. C++11 and later Standards introduced a more consistent time library.

Example of Formatting Time Points in C++

The following example demonstrates how to format a time point using C++:

#include #include #include int main() { // Get the current time point auto now = std::chrono::system_clock::now(); // Convert it to time_t std::time_t now_time_t = std::chrono::system_clock::to_time_t(now); // Format the time using std::put_time std::cout << "Current time: " << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S") << std::endl; return 0; }

C++ time formatting std::chrono std::put_time time points C++ examples