Python模块之time模块

 time 模块

 import time

 1 #时间戳
 2 ##time.time()
 3 print(time.time()) #1528076915.5626094
 4 
 5 ##将时间戳转换为结构化时间,如无参数默认取当前时间戳
 6 ##time.localtime()  #
 7 t = time.localtime() 
 8 print(t)  #time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=9, tm_min=53, tm_sec=26, tm_wday=0, tm_yday=155, tm_isdst=0)
 9 #也可以单独取出
10 print(t.tm_year) #2018
11 print(t.tm_mon) #6
12 
13 
14 ##结构化时间
15 ##time.gmtime()
16 ##用法类似localtime,只不过tm_hour是utc时间
17 
18 ##将结构化时间转换为时间戳
19 ##time.maketime()
20 print(time.mktime(time.localtime()))  #1528077429.0
21 
22  
23 ##将结构化时间转换为字符串时间
24 ##time.strftime()
25 print(time.strftime("%Y-%m-%d %X",time.localtime()))  #2018-06-04 10:09:49
26 
27 ##将字符串时间转换为结构化时间
28 ##time.strptime()
29 print(time.strptime("2018-06-04 10:09:49","%Y-%m-%d %X")) #time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=10, tm_min=9, tm_sec=49, tm_wday=0, tm_yday=155, tm_isdst=-1)
30 
31 ##,将结构化时间转换字符串时间的快捷方法,无参数默认用time.localtime()
32 ##time.asctime
33 print(time.asctime(time.localtime())) #Mon Jun  4 10:21:09 2018
34 print(time.asctime()) #Mon Jun  4 10:21:09 2018
35 
36 #将时间戳转换为字符串时间的的快捷方法,无参数默认使用time.time() 
37 ##time.ctime()
38 print(time.ctime(time.time())) #Mon Jun  4 10:22:58 2018
39 print(time.ctime()) #Mon Jun  4 10:22:58 2018
40 
41 
42 ##time.sleep(3),睡3秒
43 
44 #datetime
45 import datetime
46 print(datetime.datetime.now())    #2018-06-04 10:25:43.717205
time模块
 1 #时间戳
 2 ##time.time()
 3 print(time.time()) #1528076915.5626094
 4 
 5 ##将时间戳转换为结构化时间,如无参数默认取当前时间戳
 6 ##time.localtime()  #
 7 t = time.localtime() 
 8 print(t)  #time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=9, tm_min=53, tm_sec=26, tm_wday=0, tm_yday=155, tm_isdst=0)
 9 #也可以单独取出
10 print(t.tm_year) #2018
11 print(t.tm_mon) #6
12 
13 
14 ##结构化时间
15 ##time.gmtime()
16 ##用法类似localtime,只不过tm_hour是utc时间
17 
18 ##将结构化时间转换为时间戳
19 ##time.maketime()
20 print(time.mktime(time.localtime()))  #1528077429.0
21 
22  
23 ##将结构化时间转换为字符串时间
24 ##time.strftime()
25 print(time.strftime("%Y-%m-%d %X",time.localtime()))  #2018-06-04 10:09:49
26 
27 ##将字符串时间转换为结构化时间
28 ##time.strptime()
29 print(time.strptime("2018-06-04 10:09:49","%Y-%m-%d %X")) #time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=10, tm_min=9, tm_sec=49, tm_wday=0, tm_yday=155, tm_isdst=-1)
30 
31 ##,将结构化时间转换字符串时间的快捷方法,无参数默认用time.localtime()
32 ##time.asctime
33 print(time.asctime(time.localtime())) #Mon Jun  4 10:21:09 2018
34 print(time.asctime()) #Mon Jun  4 10:21:09 2018
35 
36 #将时间戳转换为字符串时间的的快捷方法,无参数默认使用time.time() 
37 ##time.ctime()
38 print(time.ctime(time.time())) #Mon Jun  4 10:22:58 2018
39 print(time.ctime()) #Mon Jun  4 10:22:58 2018
40 
41 
42 ##time.sleep(3),睡3秒
43 
44 #datetime
45 import datetime
46 print(datetime.datetime.now())    #2018-06-04 10:2

猜你喜欢

转载自www.cnblogs.com/zh22333/p/9131276.html