String representation of time_t?

前端 未结 9 796
悲&欢浪女
悲&欢浪女 2020-12-09 02:05
time_t seconds;
time(&seconds);

cout << seconds << endl;

This gives me a timestamp. How can I get that epoch date into a string?<

9条回答
  •  生来不讨喜
    2020-12-09 02:34

    Try this if you want to have the time in a readable string:

    #include 
    
    std::time_t now = std::time(NULL);
    std::tm * ptm = std::localtime(&now);
    char buffer[32];
    // Format: Mo, 15.06.2009 20:20:00
    std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm);  
    

    For further reference of strftime() check out cppreference.com

提交回复
热议问题