为什么总不能正确取出系统时间:?

yushulei 2003-04-15 01:32:12
我想用这种方法取出系统时间,但一直不能如愿!请指教:

SYSTEMTIME st;
GetSystemTime(&st);
CString str;
str.Format("%u:%u:%u",st.wHours,st.wMinutes,st.wSeconds);

运行后分和秒都正确,只有小时不对,总是相差8个小时,不知是为什么?
请高手指教!
...全文
58 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
laolaoliu2002 2003-04-15
  • 打赏
  • 举报
回复
SYSTEMTIME time;
GetSystemTime(&time);
CString str;
str.Format("%d%d%d%d%d%d",time.wYear,
time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond);
SetDlgItemText(IDC_EDIT1,str);
yushulei 2003-04-15
  • 打赏
  • 举报
回复
好了,心情舒服多了,刚才真实郁闷!多谢了,各位!现在就结贴!
loopyifly 2003-04-15
  • 打赏
  • 举报
回复
SYSTEMTIME NowTime ;
GetLocalTime(&NowTime);

CString strNewFileName;
strNewFileName .Format ("%d月%d日%d时%d分%d秒.txt", NowTime.wMonth , NowTime.wDay , NowTime.wHour , NowTime.wHour , NowTime.wMinute , NowTime.wSecond );
yushulei 2003-04-15
  • 打赏
  • 举报
回复
对,有道理,应该是这个原因。不过格林尼治时间好象和我们差7个小时吧!具体我也搞不清!
hdsunwind 2003-04-15
  • 打赏
  • 举报
回复
因为GetSystemTime取的是UTC时间,而我们这里的时间和UTC时间刚好相差8个小时,所以会发生你所说的。改用CTime::GetCurrentTime或者GetLocalTime就可以了!
databasesql 2003-04-15
  • 打赏
  • 举报
回复
The GetSystemTime function retrieves the current system date and time. The system time is expressed in Coordinated Universal Time (UTC).

上面是MSDN中的说明,好象取出的时间是通用协调时间,我感觉就是所说的格林尼治时间,要+8转化为东8区的北京时间。
yushulei 2003-04-15
  • 打赏
  • 举报
回复
这个我知道,但我不知道我那种方法为什么不行。所以一直很郁闷。
因为他年月日,分秒都对,就小时不对,一直找不出原因,气刹我了!
JeasonZhao 2003-04-15
  • 打赏
  • 举报
回复
具体原因我不是很清楚,可以使用GetLocalTime解决此类问题,另外既然你使用MFC使用
CTime t = CTime::GetCurrentTime()也是不错的解决办法
在上面代码中你的变量st的属性wHours,wMinutes,wSeconds中是不是多了个s???
numen27 2003-04-15
  • 打赏
  • 举报
回复
The system time is expressed in Coordinated Universal Time (UTC).
MSDN上这么说的,取出的时间是格林尼治时间,所以相差8小时,用CTime就不会这样了
databasesql 2003-04-15
  • 打赏
  • 举报
回复
你可以用这个

CTime time= CTime::GetCurrentTime();
CString str;
str.Format("%d:%d:%d",time.GetHour(),time.GetMinute(),time.GetSecond());
numen27 2003-04-15
  • 打赏
  • 举报
回复
这样取吧:
CTime currentTime;
currentTime = CTime::GetCurrentTime();
CString strTemp;
strTemp.Format("%u,%u,%u",currentTime.GetHour(),currentTime.GetMinute(),currentTime.GetSecond());

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧