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

后端 未结 24 1963
刺人心
刺人心 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:34

    C++ shares its date/time functions with C. The tm structure is probably the easiest for a C++ programmer to work with - the following prints today's date:

    #include 
    #include 
    
    int main() {
        std::time_t t = std::time(0);   // get time now
        std::tm* now = std::localtime(&t);
        std::cout << (now->tm_year + 1900) << '-' 
             << (now->tm_mon + 1) << '-'
             <<  now->tm_mday
             << "\n";
    }
    

提交回复
热议问题