繁体   English   中英

Python以分钟为单位查找两个时间戳之间的差异

[英]Python finding difference between two time stamps in minutes

我如何在几分钟内找到两个时间戳之间的差异。 例如:-

timestamp1=2016-04-06 21:26:27
timestamp2=2016-04-07 09:06:02
difference = timestamp2-timestamp1
= 700 minutes (approx)

使用datetime模块:

from datetime import datetime

fmt = '%Y-%m-%d %H:%M:%S'
tstamp1 = datetime.strptime('2016-04-06 21:26:27', fmt)
tstamp2 = datetime.strptime('2016-04-07 09:06:02', fmt)

if tstamp1 > tstamp2:
    td = tstamp1 - tstamp2
else:
    td = tstamp2 - tstamp1
td_mins = int(round(td.total_seconds() / 60))

print('The difference is approx. %s minutes' % td_mins)

输出是:

The difference is approx. 700 minutes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM