首页 > 解决方案 > 计算两个日期时间之间的差异?

问题描述

from datetime import datetime
import pytz

cur_time = datetime.now()
cur_time = pytz.utc.localize(cur_time)

rr_name = 'xx_20211005141746.txt'
rr_time =  re.search('_(.*).txt', rr_name).group(1)
rr_time =  datetime.strptime(rr_time, '%Y%m%d%H%M%S')

tzone = "Europe/London"
hb_time = pytz.utc.localize(rr_time).astimezone(pytz.timezone(tzone))
                    
diff = cur_time - hb_time

l_heared = round((diff.total_seconds() / 60), 2) 

cur_time和之间的差rr_time大约是 6 分钟,也就是 360 秒。delta怎么让我77.65秒回...

请注意cur_time ,并且rr_name是本地日期时间

标签: pythondatetimetimezonetimedelta

解决方案


使用total_seconds方法:

print(f'{delta.total_seconds()} seconds')

输出:337.525332 seconds


推荐阅读