简体   繁体   中英

Python finding difference between two time stamps in minutes

How i can find the difference between two time stamps in minutes . for example:-

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

Using the datetime module:

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)

Output is:

The difference is approx. 700 minutes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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