How to get current time and date in C++?

后端 未结 24 1480
刺人心
刺人心 2020-11-22 06:55

Is there a cross-platform way to get the current date and time in C++?

相关标签:
24条回答
  • 2020-11-22 07:40

    There's always the __TIMESTAMP__ preprocessor macro.

    #include <iostream>
    
    using namespace std
    
    void printBuildDateTime () {
        cout << __TIMESTAMP__ << endl;
    }
    
    int main() {
        printBuildDateTime();
    }
    

    example: Sun Apr 13 11:28:08 2014

    0 讨论(0)
  • 2020-11-22 07:41

    (For fellow googlers)

    There is also Boost::date_time :

    #include <boost/date_time/posix_time/posix_time.hpp>
    
    boost::posix_time::ptime date_time = boost::posix_time::microsec_clock::universal_time();
    
    0 讨论(0)
  • 2020-11-22 07:43

    http://www.cplusplus.com/reference/ctime/strftime/

    This built-in seems to offer a reasonable set of options.

    0 讨论(0)
  • 2020-11-22 07:45

    std C libraries provide time(). This is seconds from the epoch and can be converted to date and H:M:S using standard C functions. Boost also has a time/date library that you can check.

    time_t  timev;
    time(&timev);
    
    0 讨论(0)
  • 2020-11-22 07:45

    The ffead-cpp provides multiple utility classes for various tasks, one such class is the Date class which provides a lot of features right from Date operations to date arithmetic, there's also a Timer class provided for timing operations. You can have a look at the same.

    0 讨论(0)
  • 2020-11-22 07:45
    #include <Windows.h>
    
    void main()
    {
         //Following is a structure to store date / time
    
    SYSTEMTIME SystemTime, LocalTime;
    
        //To get the local time
    
    int loctime = GetLocalTime(&LocalTime);
    
        //To get the system time
    
    int systime = GetSystemTime(&SystemTime)
    
    }
    
    0 讨论(0)
提交回复
热议问题