C++ Convert Timestamp to String: Quick Guide

You can convert a timestamp to a string using the strftime() function. Here is a simple example code:

#include <iostream>
#include <ctime>

int main() {
    time_t timestamp = time(nullptr);
    struct tm * timeinfo = localtime(&timestamp);
    
    char buffer[80];
    strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
    
    std::cout << "当前时间为:" << buffer << std::endl;
    
    return 0;
}

In this example, we first get the current timestamp and convert it to a tm structure using the localtime() function. We then use the strftime() function to format the tm structure into the specified string format and store the result in a buffer. Finally, we output the converted time string.

bannerAds