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;
}

 

posted @ 2014-08-18 21:45  zzyoucan  阅读(259)  评论(0编辑  收藏  举报