1、MFC 获取当前系统时间
CTime time = CTime::GetCurrentTime(); //获取系统日期
CString strDate = time.Format(_T("%x")); //格式化日期
CString strTime = time.Format(_T("%X")); //格式化时间
CString date = time.Format(_T("%Y-%m-%d %H:%M:%S %W-%A"));//2006-10-13 17:23:47 41-Friday
Format函数的格式符号及其意义:
- %a: 简写的星期名,例如Sat代表Saturday
- %A: 全写星期(英文),如Friday
- %b: 简写的月名,例如Mar代表March
- %B: 月份(全写英文),如October
- %c: 月/日/年 时:分:秒,如1/3/2012 21:19:11
- %d: 月中的天数,值为01~31
- %H: 24小时格式的小时数,值为00~23
- %I: 通常的12小时格式的小时数,值为01~12
- %j: 一年当中的第几天,值为001~336
- %m: 月数,值为01~12
- %M: 分钟数,值为00~59
- %p: 由12小时格式的时钟指示的am/pm(上午/下午)
- %S: 秒数,值为00~59
- %U: 年中的周数,值为00~53,以周日为一周的第一天
- %w: 周中的日期数,值为0~6,其中0为周日
- %W: 年中的周数,值为00~53,以周一为一周的第一天
- %x: 月/日/年,%c的前半段
- %X: 时/分/秒,%c的后半段
- %y: 不带年代前缀的年数,值为00~99
- %Y: 年份(带世纪,如2006)
- %z: 简写的时区名称
- %Z: 时区名称的全称,如果时区未知,此字符为空,如“中国标准时间”
2、MFC 时间比较
如果当前时间大于指定的时间,函数返回false
bool PubFunc::CompareTime(int nYear, int nMonth, int nDay)
{
CTime t(nYear, nMonth, nDay, 0, 0, 0);
if (CTime::GetCurrentTime() > t)
return FALSE;
return true;
}
3、Win32 获取当前系统时间
char* pBuf = new char[256];
memset(pBuf, 0, 256);
SYSTEMTIME time;
GetLocalTime(&time);
sprintf(pBuf, "%02d.%02d %02d:%02d:%02d.%03d",
time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);