标签:des blog class code color get
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
#include <time.h>//* 方法一 time_t
tt = time(NULL);//这句返回的只是一个时间cuo tm* t= localtime(&tt); printf("%d-%02d-%02d %02d:%02d:%02d\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);//* 方法二 SYSTEMTIME st = {0}; GetLocalTime(&st); printf("%d-%02d-%02d %02d:%02d:%02d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 |
个人觉得第二种还是比较实用的,而且也是最常用的~不过当计算算法耗时的时候,不要忘记second,不能只要用Milliseconds来减,不然后出现负值,若是算法耗时太长就得用minutes啦。再不然,就hours……//方案— 优点:仅使用C标准库;缺点:只能精确到秒级#include <time.h>#include <stdio.h>int main( void
){ time_t
t = time(0); char
tmp[64]; strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) ); puts( tmp ); return
0;}size_t
strftime(char
*strDest, size_t
maxsize, const
char *format, const
struct tm *timeptr);根据格式字符串生成字符串。struct
tm *localtime(const
time_t *timer);取得当地时间,localtime获取的结果由结构tm返回返回的字符串可以依下列的格式而定:%a 星期几的缩写。Eg:Tue%A 星期几的全名。 Eg: Tuesday%b 月份名称的缩写。%B 月份名称的全名。%c 本地端日期时间较佳表示字符串。%d 用数字表示本月的第几天 (范围为 00 至 31)。日期%H 用 24 小时制数字表示小时数 (范围为 00 至 23)。%I 用 12 小时制数字表示小时数 (范围为 01 至 12)。%j 以数字表示当年度的第几天 (范围为 001 至 366)。%m 月份的数字 (范围由 1 至 12)。%M 分钟。%p 以 ‘‘AM‘‘
或 ‘‘PM‘‘
表示本地端时间。%S 秒数。%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。%w 用数字表示本周的第几天 ( 0 为周日)。%x 不含时间的日期表示法。%X 不含日期的时间表示法。 Eg: 15:26:30%y 二位数字表示年份 (范围由 00 至 99)。%Y 完整的年份数字表示,即四位数。 Eg:2008%Z(%z) 时区或名称缩写。Eg:中国标准时间%% % 字符。//方案二 优点:能精确到毫秒级;缺点:使用了windows API#include <windows.h>#include <stdio.h>int
main( void
){SYSTEMTIME sys;GetLocalTime( &sys );printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);return
0;}//方案三,优点:利用系统函数,还能修改系统时间//此文件必须是c++文件#include<stdlib.h>#include<iostream>using
namespace std;void
main(){ system("time");}//方案四,将当前时间折算为秒级,再通过相应的时间换算即可//此文件必须是c++文件#include<iostream>#include<ctime>using
namespace std;int
main(){time_t
now_time;now_time = time(NULL);cout<<now_time;return
0;} |
http://www.cppblog.com/Tongy0/archive/2011/11/23/160782.html
标签:des blog class code color get
原文地址:http://www.cnblogs.com/shanguanghui/p/3712138.html