Converting a date into a string

C++:
Converting a date into a string

How to:

In modern C++, <chrono> and <iomanip> libraries are your friends for date-time operations. Here’s a quick method using std::put_time:

#include <iostream>
#include <iomanip>
#include <chrono>
#include <sstream>

int main() {
    auto now = std::chrono::system_clock::now(); // Get the current time
    auto time = std::chrono::system_clock::to_time_t(now); // Convert to time_t
    
    // Convert to tm struct for formatting
    std::tm tm = *std::localtime(&time);

    // String stream for output
    std::stringstream ss;

    ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S"); // Format: YYYY-MM-DD HH:MM:SS

    std::string date_str = ss.str(); // Convert to string

    std::cout << date_str << std::endl; // Output the date string
    return 0;
}

Sample Output (depends on current date and time):

2023-03-15 14:25:30

Deep Dive

Before <chrono> came into the picture, C++ programmers often had to wrestle with C-style time handling via <ctime>. This was less intuitive and more error-prone due to manual memory management and platform-dependent quirks.

Alternatives to std::put_time include using strftime, but that’s more C-style. Third-party libraries like Boost.Date_Time can offer more functionality at the cost of adding dependencies.

A key implementation detail is understanding the format specifiers in std::put_time, which are similar to those used in strftime. You’re mapping placeholders to date or time components — %Y for the full year, %m for the month, and so on.

See Also