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
Either the time-zone name or time zone abbreviation, depending on registry settings; 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.
Format code
Meaning
%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#%
# flag is ignored.
%#c
Long date and time representation, appropriate for current locale. For example: "Tuesday, March 14, 1995, 12:41:29".
%#x
Long date representation, appropriate to current locale. For example: "Tuesday, March 14, 1995".
// crt_times.c /* This program demonstrates these time and date functions: * _time64 _ftime64 _ctime64 asctime * _localtime64 _gmtime64 _mktime64 _tzset * _strtime _strdate strftime * * Also the global variable: * _tzname */
/* Set time zone from TZ environment variable. If TZ is not set, * the operating system is queried to obtain the default value * for the variable. */ _tzset();
/* Get UNIX-style time and display as number and string. */ _time64( <ime ); printf( "Time in seconds since UTC 1/1/70:t%ldn", ltime ); printf( "UNIX time and date:ttt%s", _ctime64( <ime ) );
/* Convert to time structure and adjust for PM if necessary. */ today = _localtime64( <ime ); if( today->tm_hour >= 12 ) { strcpy( ampm, "PM" ); today->tm_hour -= 12; } if( today->tm_hour == 0 ) /* Adjust if midnight hour. */ today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11 * characters and printf is used to trim off terminating * characters. */ printf( "12-hour time:tttt%.8s %sn", asctime( today ) + 11, ampm );
/* Print additional time information. */ _ftime64( &tstruct ); printf( "Plus milliseconds:ttt%un", tstruct.millitm ); printf( "Zone difference in hours from UTC:t%un", tstruct.timezone/60 ); printf( "Time zone name:tttt%sn", _tzname[0] ); printf( "Daylight savings:ttt%sn", tstruct.dstflag "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */ if( _mktime64( &xmas ) != (__time64_t)-1 ) printf( "Christmastttt%sn", asctime( &xmas ) );
/* Use time structure to build a customized time string. */ today = _localtime64( <ime );
/* Use strftime to build a customized time string. */ strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.n", today ); printf( tmpbuf ); }
Sample Output
OS time: 14:15:49 OS date: 02/07/02 Time in seconds since UTC 1/1/70: 1013120149 UNIX time and date: Thu Feb 07 14:15:49 2002 Coordinated universal time: Thu Feb 07 22:15:49 2002 12-hour time: 02:15:49 PM Plus milliseconds: 455 Zone difference in hours from UTC: 8 Time zone name: Pacific Standard Time Daylight savings: NO Christmas Sat Dec 25 12:00:00 1993
简单点的如下: CString msg1="aaaaaaaaaaa";
KillTimer(1);
CTime t = CTime::GetCurrentTime(); char szTime[8]; int nHour = t.GetHour(); int nMinute = t.GetMinute(); int nSecond = t.GetSecond(); wsprintf(szTime, "%02i:%02i:%02i", nHour, nMinute,nSecond);//分秒一般习惯用两位表 m_edit1=szTime; UpdateData (FALSE); SetTimer(1, 1000,NULL); msg1=t.Format("%d-%m-%y"); //可以看到format的功能 MessageBox(msg1);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)