VC++得到當前系統時間日期 GetSystemTime()

來源:互聯網
上載者:User

一、使用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聲明如下: WINBASEAPIVOIDWINAPIGetSystemTime(    __out LPSYSTEMTIME lpSystemTime    );    WINBASEAPIVOIDWINAPIGetLocalTime(    __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 0Local: 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);  //時間顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.