Python-time模块

2023-10-22 14:42:27 浏览数 (2)

代码语言:javascript复制
 1 import time
 2 import datetime
 3 
 4 print(datetime.datetime.now())  # 2020-04-23 13:47:14.056681
 5 
 6 # time.sleep(3)  # 程序停止 单位秒
 7 
 8 # 时间戳 从UNUX到今天一共的秒数
 9 begin_time = time.time()
10 print(begin_time)  # 1587620024.2249706
11 
12 # time.localtime() #本地时间,返回元组类型
13 now_time = time.localtime()
14 print(now_time)  # time.struct_time(tm_year=2020, tm_mon=4, tm_mday=23, tm_hour=13, tm_min=20, tm_sec=16, tm_wday=3,
15 # tm_yday=114, tm_isdst=0)
16 print(now_time.tm_year)  # 取其中一个
17 
18 # print(help(time.strftime))
19 
20 # time.strftime('格式',元组)格式化时间
21 strf_now_time = time.strftime('%Y-%m-%d %H:%M:%S', now_time)  # 2020-04-23 13:23:47
22 print(strf_now_time)
23 # 就是strftime的反转
24 strp_now_time = time.strptime('2020-04-23 13:33:44', '%Y-%m-%d %H:%M:%S')
25 print(
26     strp_now_time)  # time.struct_time(tm_year=2020, tm_mon=4, tm_mday=23, tm_hour=13, tm_min=33, tm_sec=44, tm_wday=3, tm_yday=114, tm_isdst=-1)
27 
28 # time.ctime()
29 # time.ctime(1234657899)
30 ctime = time.ctime()
31 ctime2 = time.ctime(1234657899)
32 print(ctime)  # Thu Apr 23 13:41:22 2020
33 print(ctime2)  # Sun Feb 15 08:31:39 2009

0 人点赞