C&C++与时间有关的变量和函数
1、SYSTEMTIME的
格式
pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载
:
typedef struct _SYSTEMTIME
{
WORD wYear;
WORD wMonth;
WORD wDayOfWeek; //The current day of the week; Sunday is 0, Monday is 1...
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
2、基于MFC类库的CTime::GetCurrentTime获取当前系统日期(yyyymmdd)和时间(hhmmss)
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
3、基于Windows API的GetSystemTime和GetLocalTime GetSystemTime(&sysTm); //获取格林威治标准时间,与北京时间相差8小时 GetLocalTime(&locTm); //获取本时区时间。如中国即为东八区北京时间
函数GetSystemTime和GetLocalTime声明如下:
VOID WINAPI GetSystemTime( __out LPSYSTEMTIME lpSystemTime); VOID WINAPI GetLocalTime( __out LPSYSTEMTIME lpSystemTime); lpSystemTime是获取系统时间的结构。
//获取系统时间。
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 0 //这种输出结果没有补0的效果
Local: 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); //时间显示
SYSTEMTIME systime;
GetSystemTime(&systime);
CString str;
str.Format("%04d-%02d-%02d %02d:%02d:%02d ",systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute, systime.wSecond);
4、用CTime类得到当前日期、时间、星期,格式化(详细讲解)2009-09-01 13:31
? 定义一个CTime类对象 CTime time;
? 得到当前时间 time = CTime::GetCurrentTime(); ? GetYear( ),GetMonth( ), GetDay( ), GetHour( ), GetMinute( ), GetSecond( ),
GetDayOfWeek( ) 返回整型(int)对应项目
? 将当前时间格式化 CString date = time.Format("%Y-%m-%d %H:%M:%S %W-%A"); 说明:
2006-10-13 17:23:47 41-Friday 1) 结果为:
2) 格式符号说明
%a —— 星期(缩写英文),如Fri;
%A —— 星期(全写英文),如Friday
%b —— 月份(缩写英文),如Oct
%B —— 月份(全写英文),如 October
%c —— 月/日/年 时:分:秒,如 10/13/06 19:17:17 %d —— 日期(1 ~ 31)
%H —— 时(24小时制)(0 ~ 23)
%I —— 时(12小时制)(0 ~ 12)
%j —— 一年当中的第几天,(1 ~ 366)
%m —— 月份(数字 1 ~ 12)
%M —— 分(0 ~ 59)
%p —— 12小时中的A M/PM指示,或者AM,或者PM
%S —— 秒(0 ~ 59)
%U —— 一年中的第几周,星期日作为每周的第一天(0 ~ 53) %w —— 星期(数字
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
示,0 ~ 6,0代表星期日)
%W —— 一年中的第几周,星期一作为每周的第一天(0 ~ 53) %x —— 月/日/年,%c的前半段
%X —— 时/分/秒,%c的后半段
%y —— 年份(不带世纪,如 06)
%Y —— 年份(带世纪,如 2006)
%z,%Z —— 时区名称或缩写,如果时区未知,此字符为空,如“中国标准时间”
%% —— %
“#”标志的含义:
? %#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% ——“#” 被忽略 ? %#c —— 把%c中的数字变成英文,再在前面加上星期, 如:“Tuesday, March 14, 1995, 12:41:29”.
? %#x —— 把%x中的数字变成英文,再在前面加上星期,如:Tuesday, March 14, 1995
? %#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y —— 如果开 头为0,去掉开头的0