python datetime 计算时间差

 要用到时间差计算,网上找了好久,没有找到,就自己写了一个

import datetime

qs_time = '2019-07-01 23:46:39'
pj_time = '2019-06-28 10:03:36'
ls_time = '2019-06-25 21:17:40'

qs = datetime.datetime.strptime(qs_time,"%Y-%m-%d %H:%M:%S")
pj = datetime.datetime.strptime(pj_time,"%Y-%m-%d %H:%M:%S")
#格式化
num = str(qs-pj)
n = num.replace('days','天')
print(n)


#****************
3 天, 13:43:03

第二个版本

import datetime

qs_time = '2019-07-01 23:46:39'
pj_time = '2019-06-28 10:03:36'
ls_time = '2019-06-25 21:17:40'

qs = datetime.datetime.strptime(qs_time,"%Y-%m-%d %H:%M:%S")
pj = datetime.datetime.strptime(pj_time,"%Y-%m-%d %H:%M:%S")
#格式化
num = str(qs-pj)
n = num.replace('days','天').replace(',','').replace(' ','').replace(':','小时',1).replace(':','分',1).replace(':','秒')
print(n+'秒')

#**********************
3天13小时43分03秒
[Finished in 0.2s]

 

你可能感兴趣的:(python datetime 计算时间差)