VC++得到当前系统时间日期 GetSystemTime()

一、使用MFC可以用以下代码得到:

CTime time = CTime::GetCurrentTime(); ///构造CTime对象
int m_nYear = time.GetYear(); ///年
int m_nMonth = time.GetMonth(); ///月
int m_nDay = time.GetDay(); ///日
int m_nHour = time.GetHour(); ///小时
int m_nMinute = time.GetMinute(); ///分钟
int m_nSecond = time.GetSecond(); ///秒

 


    // CTime time = CTime::GetCurrentTime();
 //CString strTime = time.Format("%Y-%m-%d %H:%M:%S");  //Format("%I:%M:%S %p, %A, %B %d, %Y")
我们还可以用CTime::Format函数将CTime对象转换为字符串对象
例如:

CString m_strTime = time.Format("%Y-%m-%d %H:%M:%S");
运行结果:m_strTime为 2001-8-1 12:11:05


函数GetSystemTime和GetLocalTime声明如下:
 
WINBASEAPI
VOID
WINAPI
GetSystemTime(
    __out LPSYSTEMTIME lpSystemTime
    );
   
WINBASEAPI
VOID
WINAPI
GetLocalTime(
    __out LPSYSTEMTIME lpSystemTime
    );
 
lpSystemTime是获取系统时间的结构。
 
调用函数的例子如下:
 
  //获取系统时间。
  //蔡军生 2007/11/11 QQ:9073204 深圳
  void TestSystem(void)
  {
         //获取系统的UTC时间。
         SYSTEMTIME stUTC;
        ::GetSystemTime(&stUTC);
 
         //显示时间的间隔。
         const int nBufSize = 256;
         TCHAR chBuf[nBufSize];
         wsprintf(chBuf,_T("UTC: %u/%u/%u %u:%u:%u:%u %d\r\n"),            
               stUTC.wYear, stUTC.wMonth, stUTC.wDay,
               stUTC.wHour, stUTC.wMinute, stUTC.wSecond,
               stUTC.wMilliseconds,stUTC.wDayOfWeek);
         OutputDebugString(chBuf);
 
 
         //获取当地的时间。
         SYSTEMTIME stLocal;
        ::GetLocalTime(&stLocal);
 
         //显示时间的间隔。
         wsprintf(chBuf,_T("Local: %u/%u/%u %u:%u:%u:%u %d\r\n"),                 
               stLocal.wYear, stLocal.wMonth, stLocal.wDay,
               stLocal.wHour, stLocal.wMinute, stLocal.wSecond,
               stLocal.wMilliseconds,stLocal.wDayOfWeek);
         OutputDebugString(chBuf);
 
  }
 
 
上面两个函数在我测试时输出的结果,如下:
UTC: 2007/11/11 1:53:1:46 0
Local: 2007/11/11 9:53:1:46 0
 
strTime.Format(_T("%d-%d-%d %d:%d:%d.%d "),stLocal.wYear,stLocal.wMonth,stLocal.wDay,stLocal.wHour,stLocal.wMinute,stLocal.wSecond,stLocal.wMilliseconds);  //时间显示

posted on 2011-04-12 10:49  carekee  阅读(3507)  评论(0编辑  收藏  举报