datetime日期和时间
datetime是Python处理日期和时间的标准库。
1 from datetime import datetime 2 3 # 获取当前时间 4 now = datetime.now() 5 print(now) 6 # 结果:2018-03-29 11:25:28.103611 7 8 # 获取指定日期和时间 9 t = datetime(2017, 3, 7, 13, 32, 33) 10 print(t) 11 # 结果:2017-03-07 13:32:33 12 13 # 将时间转化为时间戳 14 now = datetime.now() 15 print(now.timestamp()) 16 # 结果:1522294205.278572 17 18 # 将时间戳转化为可读时间 19 t = 1522294205.278572 20 print(datetime.fromtimestamp(t)) 21 # 结果:2018-03-29 11:30:05.278572