Current date and time as string

后端 未结 6 1838
梦毁少年i
梦毁少年i 2020-12-04 11:09

I wrote a function to get a current date and time in format: DD-MM-YYYY HH:MM:SS. It works but let\'s say, its pretty ugly. How can I do exactly the sam

相关标签:
6条回答
  • 2020-12-04 11:38

    I wanted to use the C++11 answer, but I could not because GCC 4.9 does not support std::put_time.

    std::put_time implementation status in GCC?

    I ended up using some C++11 to slightly improve the non-C++11 answer. For those that can't use GCC 5, but would still like some C++11 in their date/time format:

     std::array<char, 64> buffer;
     buffer.fill(0);
     time_t rawtime;
     time(&rawtime);
     const auto timeinfo = localtime(&rawtime);
     strftime(buffer.data(), sizeof(buffer), "%d-%m-%Y %H-%M-%S", timeinfo);
     std::string timeStr(buffer.data());
    
    0 讨论(0)
  • 2020-12-04 11:47

    Non C++11 solution: With the <ctime> header, you could use strftime. Make sure your buffer is large enough, you wouldn't want to overrun it and wreak havoc later.

    #include <iostream>
    #include <ctime>
    
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
      char buffer[80];
    
      time (&rawtime);
      timeinfo = localtime(&rawtime);
    
      strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
      std::string str(buffer);
    
      std::cout << str;
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-12-04 11:47

    you can use asctime() function of time.h to get a string simply .

    time_t _tm =time(NULL );
    
    struct tm * curtime = localtime ( &_tm );
    cout<<"The current date/time is:"<<asctime(curtime);
    

    Sample output:

    The current date/time is:Fri Oct 16 13:37:30 2015
    
    0 讨论(0)
  • 2020-12-04 11:48
    #include <chrono>
    #include <iostream>
    
    int main()
    {
        std::time_t ct = std::time(0);
        char* cc = ctime(&ct);
    
        std::cout << cc << std::endl;
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-04 11:53

    Since C++11 you could use std::put_time from iomanip header:

    #include <iostream>
    #include <iomanip>
    #include <ctime>
    
    int main()
    {
        auto t = std::time(nullptr);
        auto tm = *std::localtime(&t);
        std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
    }
    

    std::put_time is a stream manipulator, therefore it could be used together with std::ostringstream in order to convert the date to a string:

    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <sstream>
    
    int main()
    {
        auto t = std::time(nullptr);
        auto tm = *std::localtime(&t);
    
        std::ostringstream oss;
        oss << std::put_time(&tm, "%d-%m-%Y %H-%M-%S");
        auto str = oss.str();
    
        std::cout << str << std::endl;
    }
    
    0 讨论(0)
  • 2020-12-04 11:53

    Using C++ in MS Visual Studio 2015 (14), I use:

    #include <chrono>
    
    string NowToString()
    {
      chrono::system_clock::time_point p = chrono::system_clock::now();
      time_t t = chrono::system_clock::to_time_t(p);
      char str[26];
      ctime_s(str, sizeof str, &t);
      return str;
    }
    
    0 讨论(0)
提交回复
热议问题