VC中得到当前系统的时间和日期

得到时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒)。主要方法如下:
1)使用CRT函数
char szCurrentDateTime[32];   
time_t nowtime;   
struct tm* ptm;   
time(&nowtime);   
ptm = localtime(&nowtime);   
sprintf(szCurrentDateTime, "%4d-%.2d-%.2d %.2d:%.2d:%.2d",   
    ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,   
    ptm->tm_hour, ptm->tm_min, ptm->tm_sec);  


2)使用SYSTEMTIME
char szCurrentDateTime[32];   
SYSTEMTIME systm;   
GetLocalTime(&systm);   
sprintf(szCurrentDateTime, "%4d-%.2d-%.2d %.2d:%.2d:%.2d",   
        systm.wYear, systm.wMonth, systm.wDay,   
        systm.wHour, systm.wMinute, systm.wSecond);  


3)使用CTime
char szCurrentDateTime[32];   
CTime nowtime;   
nowtime = CTime::GetCurrentTime();   
  
sprintf(szCurrentDateTime, "%4d-%.2d-%.2d %.2d:%.2d:%.2d",   
    nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay(),   
    nowtime.GetHour(), nowtime.GetMinute(), nowtime.GetSecond());  


你可能感兴趣的:(C++,c,C#,vc++)