1.1、日历时间:
| 格式 |
说明 |
实例 |
| %a |
缩写的星期名 |
Thu |
| %A |
全星期名 |
Thursday |
| %b |
缩写的月名 |
Aug |
| %B |
全月名 |
August |
| %c |
日期和时间 |
Thu Aug 23 14:55:02 2001 |
| %C |
年/100 [00,99] |
20 |
| %d |
十进制表示的每月的第几天 [01,31] |
16 |
| %D |
月/天/年 [MM/DD/YY] |
06/16/12 |
| %e |
十进制表示的每月的第几天 ,一位数前加空格[1,31] |
10 |
| %F |
年-月-日 [YYYY-MM-DD] |
2012-06-16 |
| %g |
ISO 8601使用基于周的年的后两位数字 |
12 |
| %G |
ISO 8601使用基于周的年 |
2012 |
| %h |
简写的月份名 ,与%b相同 |
Aug |
| %H |
24小时制的小时 [00,23] |
14 |
| %I |
12小时制的小时 [01,12] |
02 |
| %j |
每年的第几天 [001,366] |
235 |
| %m |
十进制的月 [01,12] |
08 |
| %M |
分钟 [00,59] |
55 |
| %n |
换行符 |
|
| %p |
AM/PM |
PM |
| %r |
本地时间:(12时制) |
11:01:23 AM |
| %R |
与“%H:%M”相同 |
11:01 |
| %S |
秒 [00,60] |
02 |
| %t |
水平制表符 |
|
| %T |
与“%H:%M:%S”相同 |
11:01:23 |
| %u |
ISO 8601的星期,星期一为1,[1,7] |
2 |
| %U |
周数 ,以周日为一周开始 [00,53] |
33 |
| %V |
ISO 8601周数 [01,53] |
07 |
| %w |
星期,星期天为0. [0,6] |
4 |
| %W |
周数,以星期一为一周开始 [00,53] |
34 |
| %x |
标准日期 |
06/16/12 |
| %X |
标准时间 |
14:55:02 |
| %y |
年份的后两位数字 [00,99] |
12 |
| %Y |
年 |
2012 |
| %z |
ISO 8601格式的UTC偏移量 |
-0500 |
| %Z |
时区名 |
EST |
| %% |
百分号 |
% |
- #include<stdio.h>
- #include<time.h>
- int main()
- {
- //获取当前日历时间
- time_t t;
- t = time(&t);
- printf("the current time of seconds:[%d], string is:[%s]\n",t, ctime(&t));
- //日历时间转成本地tm结构时间
- struct tm* tm_t = localtime(&t);
- char buf[100];
- strftime(buf, 100,"%F %T",tm_t);
- printf("get struct tm tm_t from seconds is:[%s]\n", buf);
- //tm结构时间转成日历时间
- time_t t1 = mktime(tm_t);
- printf("get seconds from struct tm:[%d]\n",t);
- //自定义tm结构时间
- struct tm tm_t1;
- tm_t1.tm_year=2012-1900;
- tm_t1.tm_mon=0;
- tm_t1.tm_mday=16;
- tm_t1.tm_hour=0;
- tm_t1.tm_sec=0;
- tm_t1.tm_min=0;
- strftime(buf, 100,"%F %T",&tm_t1);
- printf("the struct tm tm_t1 is:[%s]\n", buf);
- }
]
get struct tm tm_t from seconds is:[2012-06-15 21:10:19]
get seconds from struct tm:[1339819819]
the struct tm tm_t1 is:[2012-01-16 00:00:00]
