给出两个时间点
t_1 = (hour_1, minute_1)
t_2 = (hour_2, minute_2)
如何在Python 2.7中获得绝对时间差,以分钟为单位
我假设t_2
晚于t_1
,所以如果:
t_2 = (0, 3) and t_1 = (23, 59)
差异将是4分钟。
答案 0 :(得分:2)
t_2 = (0, 3)
t_1 = (23, 59)
from datetime import timedelta
t1 = timedelta(hours=t_1[0],minutes=t_1[1])
t2 = timedelta(hours=t_2[0],minutes=t_2[1])
print((t2-t1).seconds/60)
4.0
答案 1 :(得分:0)
使用datetime.timedelta
个对象(此处t1
大于t2
):
>>> import datetime
>>> t1 = (10, 0)
>>> t2 = (5, 5)
>>> d1 = datetime.timedelta(seconds=60 * 60 * t1[0] + 60 * t1[1])
>>> d2 = datetime.timedelta(seconds=60 * 60 * t2[0] + 60 * t2[1])
>>> d1 - d2
datetime.timedelta(0, 17995)
>>> (d1 - d2).total_seconds()
17995.0