Getting the current time as a YYYY-MM-DD-HH-MM-SS string

后端 未结 2 972
孤街浪徒
孤街浪徒 2020-12-31 01:17

I\'m trying to get the current time as a \"YYYY-MM-DD-HH-MM-SS\" formatted string in an elegant way. I can take the current time in ISO format from Boost\'s \"Date Time\" li

相关标签:
2条回答
  • 2020-12-31 01:57

    The answer depends on what you mean by get and take. If you are trying to output a formatted time string, use strftime(). If you are trying to parse a text string into a binary format, use strptime().

    0 讨论(0)
  • 2020-12-31 02:02

    Use std::strftime, it is standard C++.

    #include <cstdio>
    #include <ctime>
    
    int main ()
    {
        std::time_t rawtime;
        std::tm* timeinfo;
        char buffer [80];
    
        std::time(&rawtime);
        timeinfo = std::localtime(&rawtime);
    
        std::strftime(buffer,80,"%Y-%m-%d-%H-%M-%S",timeinfo);
        std::puts(buffer);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题