VC++获取系统当前时间并显示

VC++6.0 获取系统时间 并输入格式化字符串  

CTime tm = CTime::GetCurrentTime();
   CString strtm= tm.Format("%Y-%m-%d %H:%M:%S");
   AfxMessageBox(strtm);

 

1、定义一个CTime类对象 CTime time;

2、 得到当前时间 time = CTime::GetCurrentTime();

3、GetYear( ),GetMonth( ), GetDay( ), GetHour( ), GetMinute( ), GetSecond( ), GetDayOfWeek( ) 返回整型(int)对应项目

4、将当前时间格式化 CString date = time.Format("%Y-%m-%d %H:%M:%S %W-%A");

例子:

1
2
3
CTime ct = CTime::GetCurrentTime(); //获取当前时间
 
CString str = ct.Format( "%Y-%m_%d %H-%M-%S" ); //格式化时间

结果:str="2006-04-23 15-21-30"

日期格式化参数说明:

%a

Abbreviated weekday name
星期(缩写英文),如Fri;

%A

Full weekday name
星期(全写英文),如Friday

%b

Abbreviated month name
月份(缩写英文),如Oct

%B

Full month name
月份(全写英文),如 October

%c

Date and time representation appropriate for locale
月/日/年 时:分:秒,如 10/13/06 19:17:17

%d

Day of month as decimal number (01 – 31)
一月中的日期(1 ~ 31)

%H

Hour in 24-hour format (00 – 23)
时(24小时制)(0 ~ 23)

%I

Hour in 12-hour format (01 – 12)
 时(12小时制)(0 ~ 12)

%j

Day of year as decimal number (001 – 366)
一年当中的第几天,(1 ~ 366)

%m

Month as decimal number (01 – 12)
月份(数字 1 ~ 12)

%M

Minute as decimal number (00 – 59)
分(0 ~ 59)

%p

Current locale's A.M./P.M. indicator for 12-hour clock
12小时中的A M/PM指示,或者AM,或者PM

%S

Second as decimal number (00 – 59)
秒(0 ~ 59)

%U

Week of year as decimal number, with Sunday as first day of week (00 – 53)
一年中的第几周,星期日作为每周的第一天(0 ~ 53)

%w

Weekday as decimal number (0 – 6; Sunday is 0)
星期(数字表示,0 ~ 6,0代表星期日)

%W

Week of year as decimal number, with Monday as first day of week (00 – 53)
一年中的第几周,星期一作为每周的第一天(0 ~ 53)

%x

Date representation for current locale
月/日/年,%c的前半段

%X

Time representation for current locale
时/分/秒,%c的后半段

%y

Year without century, as decimal number (00 – 99)
年份(不带世纪,如 06)

%Y

Year with century, as decimal number
年份(带世纪,如 2006)

%z, %Z

Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
时区名称或缩写,如果时区未知,此字符为空,如“中国标准时间”

%%

Percent sign
两个百分号格式化以后代表一个%

============================================================
下面是另一个例子:

1
2
3
4
5
6
7
8
9
// Example for CTime::Format and CTime::FormatGmt
 
CTime t( 1999, 3, 19, 22, 15, 0 );
 
// 10:15 PM March 19, 1999
 
CString s = t.Format( "%A, %B %d, %Y" );
 
ATLASSERT( s == "Friday, March 19, 1999" );

补充说明:“#”标志的含义

① %#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

你可能感兴趣的:(VC++获取系统当前时间并显示)