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

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

    localtime_s() version:

    #include 
    #include 
    
    int main ()
    {
      time_t current_time;
      struct tm  local_time;
    
      time ( ¤t_time );
      localtime_s(&local_time, ¤t_time);
    
      int Year   = local_time.tm_year + 1900;
      int Month  = local_time.tm_mon + 1;
      int Day    = local_time.tm_mday;
    
      int Hour   = local_time.tm_hour;
      int Min    = local_time.tm_min;
      int Sec    = local_time.tm_sec;
    
      return 0;
    } 
    

提交回复
热议问题