Kush
Kush

Reputation: 39

How to find difference between two timestamps in Python?

I need to find the difference between two timestamps in minutes. I am using Python 3.6.

Here is my script:

import datetime
from dateutil import parser

indate = str(datetime.datetime.utcnow())
indate2 = parser.parse(indate)
indate3  = indate2.date()
intime = indate2.time()


outdate1 = "2019-10-16T06:38:55.000+0000"
outdate2 = parser.parse(outdate1)
outdate3  = outdate2.date()
outtime = outdate2.time()

### ---THEN  PRINT DIFFERENCE BETWEEN THE TWO IN MINUTES --- ###

Upvotes: 2

Views: 371

Answers (2)

Frank
Frank

Reputation: 2029

You need to remove the timezone awarenes from outdate2

print(indate2 - outdate2.replace(tzinfo=None))

Upvotes: 2

Onyambu
Onyambu

Reputation: 79228

It will be advisable to ensure that they both have the same timezone:

 (indate2.astimezone(datetime.timezone.utc) - outdate2).total_seconds()/60
    Out[161]: 494.60840941666663

Upvotes: 2

Related Questions