首页 > 其他分享 >CTime类format的使用

CTime类format的使用

时间:2023-01-27 21:32:31浏览次数:35  
标签:printf 12 CTime format decimal 使用 number %# time

<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.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=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
%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 #


Format code

Meaning

%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#%

#

%#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( <ime ); printf( "Time in seconds since UTC 1/1/70:/t%ld/n", ltime ); printf( "UNIX time and date:/t/t/t%s", _ctime64( <ime ) ); /* Display UTC. */ gmt = _gmtime64( <ime ); printf( "Coordinated universal time:/t/t%s", asctime( gmt ) ); /* 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:/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( <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: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);

标签:printf,12,CTime,format,decimal,使用,number,%#,time
From: https://blog.51cto.com/u_15942605/6024356

相关文章

  • Git安装与使用
     第一步到这个地址下载相应系统的Git进行安装      ​​https://git-scm.com/download/win​​ 安装很简单,除了下面这里手动选择“usegitfromGitBashonly......
  • [转]python库 Pywin32使用
    本文转自:https://www.cnblogs.com/chenjy1225/p/12174889.htmlpython库Pywin32使用 https://github.com/wuxc/pywin32docPywin32提供了很多访问windows的API。较重......
  • 使用provisio-maven-plugin+ airlift launcher 开发类似trino 的运行包
    如果运行过trino或者presto会发现比较方便,配置放的特别清晰,而且包含了方便的cli工具,实际上trino或者presto内部也是基于了provisio-maven-plugin+airliftlauncher......
  • 如何使用adb 安装 apk应用
    如何使用adb安装apk应用首先下载adb软件;点击下载 安装软件,并添加到环境变量里面;手机开启调试模式cmd输入adbdevice,手机界面同意调试;输入安装命令adb......
  • Latex使用总结(三)
    问题与方法表格的数据需要垂直居中,如文本+数字的显示,由于文本的长度内容多于数字,因此数字的显示需要在文本的中央。可以在tabular中设置,使用m的方法,注意:需要在文本这一栏......
  • 递归函数的全局变量使用技巧
    递归函数的全局变量使用技巧我希望提取以下数组中每个path的值放入一个数组letarr=[{path:'a',b:2,children:[{......
  • 【转】springmvc 使用logback
    一、概述:步骤:第一步加入相关pom文件;步骤二:添加logback.xml配置;步骤三:web.xml添加日志监听 二、pom.xml加入jar包1<!--Logback-->2<!--https://......
  • 扩展springboot的json数据返回使用的json序列化方式
    扩展springboot的json数据返回使用的json序列化方式可以指定时间类型json化后的返回样式可以指定特殊功能定义,Long类型转成字符串形式返回自定义序列化转换器,代码范......
  • 使用 Winget 命令在 Windows 上安装焰火十二卷(Rickrack)
    Winget是微软推出的一款安装包管理工具,使用户能够在命令行下寻找、安装、升级、删除和配置应用程序。这里以安装焰火十二卷(Rickrack)为例,演示了Winget的使用流程。寻找......
  • spring boot——请求与参数校验——使用ResponseEntity处理http响应
                packageorg.example.controller.requestparam;importorg.apache.ibatis.jdbc.Null;importorg.springframework.http.HttpHea......