Sunday Blog

人生是一场刻意练习

Python Datetime 时间函数

Python Datetime

import datetime datestr ='2021-06-10 15:16:08' dt = datetime.datetime.strptime(datestr, '%Y-%m-%d %H:%M:%S') print(type(dt), dt) print(dt.strftime( '%/%m/%d-%H:%M:%S')) # 输出为字符串 print("{:%Y/%m/%d %H:%M:%S}" . format(dt)) # 输出为字符串 <class 'datetime.datetime'> 2021-06-10 15:16:08 /06/10-15:16:08 2021/06/10 15:16:08 import datetime d1=datetime.datetime.now() #无时区时间 print(d1) d2=datetime.datetime.utcnow() #UTC时间 print(d2) d3=datetime.datetime(2018,8,8,10,5,39) print(d3) 2022-08-12 14:29:35.393445 2022-08-12 06:29:35.393494 2018-08-08 10:05:39 时区 # timezone 时