c++ 时间处理

#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    //tm转换成字符串
    time_t t = time(0);
    tm tm;
    localtime_s(&tm, &t);  

    char szTime[64];
    sprintf_s( szTime, 64, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d",  
        tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,  
        tm.tm_hour, tm.tm_min,tm.tm_sec);  

    //字符串转换成tm
    sscanf_s(szTime, "%4d-%2d-%2d %2d:%2d:%2d",      
        &tm.tm_year,   
        &tm.tm_mon,   
        &tm.tm_mday,   
        &tm.tm_hour,   
        &tm.tm_min,  
        &tm.tm_sec);  

    cout << szTime;
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/zzyoucan/p/3920555.html