如何以分钟为单位找出时差。
以下是时间格式t1和t2
t1 = 01/24 14:50:0
t2 = 1/24 14:52:01
差异是两分钟
如何实现这个
我尝试了以下代码但没有工作:
fmt = '%m/%d %H:%M:%S'
d1 = datetime.strptime('01/24 14:50:0', fmt)
d2 = datetime.strptime('1/24 14:52:01', fmt)
d1_ts = time.mktime(d1.timetuple())
d2_ts = time.mktime(d2.timetuple())
print (int(d1_ts-d2_ts) / 60)
答案 0 :(得分:0)
我以这种方式尝试过:
from datetime import datetime
# print("Hello Python")
t1 = "31/12/2015 10:33:26"
t2 = "01/01/2016 10:33:37"
newdate1 = datetime.strptime(t1, "%d/%m/%Y %H:%M:%S")
newdate2 = datetime.strptime(t2, "%d/%m/%Y %H:%M:%S")
thedifference = newdate2 - newdate1
print(thedifference)
# Result: 1 day, 0:00:11
确保在顶部
导入所需内容答案 1 :(得分:0)
试试这个。
from datetime import datetime
fmt = '%m/%d %H:%M:%S'
d1 = datetime.strptime('01/24 14:50:0', fmt)
d2 = datetime.strptime('1/24 14:52:01', fmt)
time_diff_in_secs = abs((d1-d2).total_seconds())
print( time_diff_in_secs/60 ) #returns float
#for int use, print( int(time_diff_in_secs/60) )