700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > VC++得到当前系统时间日期 GetSystemTime()

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

时间:2024-06-26 21:02:06

相关推荐

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是获取系统时间的结构。 调用函数的例子如下:// 获取系统时间。 // 蔡军生 /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: /11/111:53:1:46 0 Local: /11/119: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); //时间显示

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。