CTime类format的使用

简介: CTime类format的使用
<script>function StorePage(){d=document;t=d.selection?
(d.selection.type!='None'?
d.selection.createRange().text:''):(d.getSelection?
d.getSelection():'');void(keyit=window.open('http://www.3
65key.com/storeit.aspx?
t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='
+escape(t),'keyit','scrollbars=no,width=475,height=575,lef
t=75,top=20,status=no,resizable=yes'));keyit.focus();}
</script>


使用CTime类可以很方便地取得当前系统时间并转换为各种格式

   

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".
%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y Remove leading zeros (if any).


Requirements


Routine Required header Compatibility
strftime <time.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
wcsftime <time.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP


Example


// 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 */#include <time.h>#include <stdio.h>#include 
<sys/types.h>#include <sys/timeb.h>#include <string.h>int main(){    char tmpbuf[128], ampm[] = 
"AM";    __time64_t ltime;    struct __timeb64 tstruct;    struct tm *today, *gmt, xmas = 
{ 0, 0, 12, 25, 11, 93 };    /* 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();    /* Display operating system-style date and time. 
*/    _strtime( tmpbuf );    printf( "OS time:/t/t/t/t%s/n", tmpbuf );    _strdate( tmpbuf 
);    printf( "OS date:/t/t/t/t%s/n", tmpbuf );    /* Get UNIX-style time and display as 
number and string. */    _time64( &ltime );    printf( "Time in seconds since UTC 
1/1/70:/t%ld/n", ltime );    printf( "UNIX time and date:/t/t/t%s", _ctime64( &ltime ) 
);    /* Display UTC. */    gmt = _gmtime64( &ltime );    printf( "Coordinated universal 
time:/t/t%s", asctime( gmt ) );    /* Convert to time structure and adjust for PM if 
necessary. */    today = _localtime64( &ltime );    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:/t/t/t/t%.8s %s/n",       asctime( today ) + 
11, ampm );    /* Print additional time information. */    _ftime64( &tstruct 
);    printf( "Plus milliseconds:/t/t/t%u/n", tstruct.millitm );    printf( "Zone 
difference in hours from UTC:/t%u/n",              tstruct.timezone/60 );    printf( "Time 
zone name:/t/t/t/t%s/n", _tzname[0] );    printf( "Daylight savings:/t/t/t%s/n",          
tstruct.dstflag ? "YES" : "NO" );    /* Make time for noon on Christmas, 
1993. */    if( _mktime64( &xmas ) != (__time64_t)-1 )   printf( "Christmas/t/t/t/t%s/n", 
asctime( &xmas ) );    /* Use time structure to build a customized time string. 
*/    today = _localtime64( &ltime );    /* 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:49OS 
date:                                02/07/02Time in seconds since UTC 1/1/70:  
1013120149UNIX time and date:                     Thu Feb 07 14:15:49 2002Coordinated universal 
time:             Thu Feb 07 22:15:49 200212-hour time:                           02:15:49 PMPlus milliseconds:                      455Zone 
difference in hours from UTC:      8Time zone name:                         Pacific 
Standard TimeDaylight savings:                       
NOChristmas                               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);


相关文章
|
1月前
DATE_FORMAT函数使用
DATE_FORMAT函数使用
186 0
|
7月前
|
关系型数据库 MySQL PostgreSQL
PSQLException: 错误: 函数 date_format(timestamp without time zone, unknown) 不存在
PSQLException: 错误: 函数 date_format(timestamp without time zone, unknown) 不存在
136 0
|
10月前
|
Java
time类获取时间
time类获取时间
88 0
|
SQL
format函数
format函数
115 0
|
SQL MySQL 关系型数据库
mysql DATE_FORMAT(date, format) 函数
DATE_FORMAT(date, format) 函数用法   DATE_FORMAT(date, format) 函数根据format字符串格式化date值。   1.把字符串转为日期格式 实例: SELECT DATE_FORMAT('2017-09-20 08:30:45', '%Y-...
3132 0
resolveType - when is date type for DateFormat used when initialization
Created by Wang, Jerry, last modified on Oct 27, 2015
120 0
resolveType - when is date type for DateFormat used when initialization
【转载】format的用法。
以前没太注意这个用法,到网上找一个,copy过来,方便以后的查看。   "I see stuff like {0,-8:G2} passed in as a format string. What exactly does that do?" -- Very Confused String Fo...
884 0
|
Python
time模块 - 日期转换
==== 模块名称:time ==== 一种获取当前时间,以及时间格式化的模块 time模块在Python原生安装中就存在,直接使用即可,无需额外的安装操作 == 导入方式:import time == # -*- coding: utf-8 -*- import time import locale 设置本地语言类型为中文 locale.
948 0