test Blog
Happy living

strftime()函数将时间格式化

老鼠 posted @ 2009年3月10日 02:36 in linux , 2082 阅读

我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:

size_t strftime(
     char *strDest,
     size_t maxsize,
     const char *format,
     const struct tm *timeptr
);

我们可以根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符数。

函数strftime()的操作有些类似于sprintf():识别以百分号(%)开始的格式命令集合,格式化输出结果放在一个字符串中。格式化命令说明串 strDest中各种日期和时间信息的确切表示方法。格式串中的其他字符原样放进串中。格式命令列在下面,它们是区分大小写的。

%a 星期几的简写
%A 星期几的全称
%b 月分的简写
%B 月份的全称
%c 标准的日期的时间串
%C 年份的后两位数字
%d 十进制表示的每月的第几天
%D 月/天/年
%e 在两字符域中,十进制表示的每月的第几天
%F 年-月-日
%g 年份的后两位数字,使用基于周的年
%G 年分,使用基于周的年
%h 简写的月份名
%H 24小时制的小时
%I 12小时制的小时
%j 十进制表示的每年的第几天
%m 十进制表示的月份
%M 十时制表示的分钟数
%n 新行符
%p 本地的AM或PM的等价显示
%r 12小时的时间
%R 显示小时和分钟:hh:mm
%S 十进制的秒数
%t 水平制表符
%T 显示时分秒:hh:mm:ss
%u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)
%U 第年的第几周,把星期日做为第一天(值从0到53)
%V 每年的第几周,使用基于周的年
%w 十进制表示的星期几(值从0到6,星期天为0)
%W 每年的第几周,把星期一做为第一天(值从0到53)
%x 标准的日期串
%X 标准的时间串
%y 不带世纪的十进制年份(值从0到99)
%Y 带世纪部分的十制年份
%z,%Z 时区名称,如果不能得到时区名称则返回空字符。
%% 百分号

如果想显示现在是几点了,并以12小时制显示,就象下面这段程序:

#i nclude “time.h”
#i nclude “stdio.h”
int main(void)
{
struct tm *ptr;
time_t lt;
char str[80];
lt=time(NULL);
ptr=localtime(<);
strftime(str,100,"It is now %I %p",ptr);
printf(str);
return 0;
}

其运行结果为:
It is now 4PM

而下面的程序则显示当前的完整日期:

#i nclude
#i nclude

void main( void )
{
          struct tm *newtime;
          char tmpbuf[128];
          time_t lt1;
          time( <1 );
          newtime=localtime(<1);
          strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
          printf(tmpbuf);
}

运行结果:

Today is Saturday, day 30 of July in the year 2005.

CIBIL Score Check 说:
2022年12月20日 23:27

CIBIL ie Credit Information Bureau India Limited is a credit Information Company which was established in August 2000 before the invention of Credit Scores in 1950's. Lenders have to take the social approach to determine the creditworthiness of an applicant. CIBIL Score Check The CIBIL Score is the most important score of the adult life of an individual. It indicates if you are eligible for loan or not.

Tenda WiFi 说:
2023年2月08日 21:21

The popular brand Tenda supplies various wireless routers which well known for easy configuration of the WiFi network. Once a new Router bought, one should make it ready to configure such that their LAN connection does move to WiFi to used by multiple devices. Tenda WiFi Tenda is the most preferred brand in customer premises wireless equipment due to its good range and stable connection. The routers found in 150Mbps and 300Mbps form which bought based on customer visibility.

thezeusnetwork/activ 说:
2023年7月12日 00:01

Zeus Network provides a dedicated Zeus free trial. Sadly, Zeus does not provide a Zeus free trial period before the membership. The Zeus Network, including documentaries, drama series and reality shows. thezeusnetwork/activate Once after downloading, open the application, and “activate Zeus Network” at “thezeusnetwork.com/activate” in search of the available content. Zeus Network without a connecting TV broadcasting service provider the Zeus Network app is available on popular streaming devices.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter