一、使用MFC可以用以下代码得到
CTime time = CTime::GetCurrentTime();
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::Format函数将CTime对象转换为字符串对象
例如:
CTime time = CTime::GetCurrentTime();
CString m_strTime = time.Format("%Y-%m-%d %H:%M:%S");
运行结果:m_strTime为 2001-8-1 12:11:05
二、使用GetSystemTime()这个API函数得到系统时间
SYSTEMTIME ti;
GetSystemTime(&ti);
////我们可以通过读取SYSTEMTIME结构体成员直接得到时间
typedef struct _SYSTEMTIME {
} SYSTEMTIME, *PSYSTEMTIME;
例如:ti.wMilliseconds;可以得到毫秒时间
本文介绍了两种在Windows环境下获取当前时间的方法:一是利用MFC的CTime类,通过构造对象并调用相应方法获取年、月、日、时、分、秒等信息;二是通过调用GetSystemTime() API函数,读取SYSTEMTIME结构体成员来获取时间。
2万+

被折叠的 条评论
为什么被折叠?



