先看看代码再说吧:
void CTest9Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
myTime = CTime::GetCurrentTime();
CString myStr = myTime.Format("%Y年%m月%d日 %X");
SetDlgItemText(IDC_EDIT1,myStr);
}
仅仅是上面这个简单的代码,就可以实现输出当前的时间,具体来说就是年月日和时分秒了。
需要注意的当然是两个:
1.得到当前的时间。调用CTime::GetCurrentTime()函数来实现。
不用惊奇,这里的变量myTime是我手动加在类中的一个成员,而点击button函数中,只是对它进行了初始化。
2.输出格式的问题,以下内容节选自MSDN:
%D Total days in this CTime
%H Hours in the current day
%M Minutes in the current hour
%S Seconds in the current minute
%% Percent sign
上面的是CTime.Formate的改变后的输出格式,而下面的依旧可以用,没有改变,继承了下来。
The format argument consists of one or more codes; as in printf, the formatting codes are preceded by a percent sign (%). Characters that do not begin with % are copied unchanged to strDest. The LC_TIME category of the current locale affects the output formatting of strftime.(For more information on LC_TIME, see setlocale.) The formatting codes for strftime are listed below:
%a
Abbreviated weekday name
%A
Full weekday name
%b
Abbreviated month name
%B
Full month name
%c
Date and time representation appropriate for locale
%d
Day of month as decimal number (01 – 31)
%H
Hour in 24-hour format (00 – 23)
%I
Hour in 12-hour format (01 – 12)
%j
Day of year as decimal number (001 – 366)
%m
Month as decimal number (01 – 12)
%M
Minute as decimal number (00 – 59)
%p
Current locale’s A.M./P.M. indicator for 12-hour clock
%S
Second as decimal number (00 – 59)
%U
Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w
Weekday as decimal number (0 – 6; Sunday is 0)
%W
Week of year as decimal number, with Monday as first day of week (00 – 53)
%x
Date representation for current locale
%X
Time representation for current locale
%y
Year without century, as decimal number (00 – 99)
%Y
Year with century, as decimal number
%z, %Z
Time-zone name or abbreviation; no characters if time zone is unknown
%%
Percent sign
As in the printf function, the # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.
posted on 2010-02-05 12:46
deercoder 阅读(1080)
评论(0) 编辑 收藏 引用