python
import datetime as dt
字符串转时间
python
dt.datetime.strptime('20210901', '%Y%m%d')
dt.datetime.strptime('2021-09-01', '%Y-%m-%d')
# 以上两种方式输出相同:
2021-09-01 00:00:00
计算时间差
python
dt_2 = dt.datetime.strptime('2021-09-03 18:00', '%Y-%m-%d %H:%M') # dt_2: 2021-09-03 18:00:00
dt_1 = dt.datetime.strptime('2021-09-01 12:00', '%Y-%m-%d %H:%M') # dt_1: 2021-09-01 12:00:00
dt_delta = dt_2-dt_1 # dt_delta: 2 days, 6:00:00
d = dt_delta.days+dt_delta.seconds/(3600*24) # 相差2.25天
h = dt_delta.days*24+dt_delta.seconds/3600 # 相差54小时