strftime()函数将时间格式化
来源:互联网 发布:法规的软件 编辑:程序博客网 时间:2024/05/21 22:50
原地址:http://blog.163.com/result_2205/blog/static/139819450201021762339822/
我们可以使用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.
#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.
0 0
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime()函数将时间格式化
- strftime() 函数将时间格式化
- strftime函数将时间格式化
- strftime函数将时间格式化
- 时间格式化函数strftime
- 时间格式化函数--strftime
- strftime() 函数---> 时间格式化
- C语言格式化输出时间函数strftime()
- strftime()函数,C语言时间格式化
- strftime 时间格式化
- linux 时间格式化函数strftime和strptime使用
- 图像存储模式
- Linux中rz和sz命令的安装及使用
- <Android笔试面试题>ANR异常
- mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息
- easyui使用mergeCells合并单元格后第一行行高过大
- strftime()函数将时间格式化
- HDU 5401(计数dp)
- Set 接口没有自己特有的方法
- HDU 1235 统计同成绩学生人数(水~)
- 【java】垃圾回收机制
- Android-模拟电视屏幕开关机特效
- NewNimGame(博弈)
- Gradle笔记——构建基础
- 欢迎使用CSDN-markdown编辑器