0

Having trouble understanding the difference between two time stamps in ISO 8061:

I have two strings:

alert_time
'2019-08-21T22:05:59.605Z'

current_time
'2019-08-21T22:06:58'

i change the alert_time variable to match the formatting of current_time

alert_time = alert_time[:-5]

alert_time
'2019-08-21T22:05:59'

Now i want to see the difference between them

datetimeFormat = '%Y-%m-%dT%H:%M:%S'
 diff = datetime.datetime.strptime(alert_time, datetimeFormat) - datetime.datetime.strptime(current_time, datetimeFormat)

diff
datetime.timedelta(-1, 86341)

what does that output mean? I'm looking for something to tell me the difference in seconds

Dave0
  • 133
  • 7
  • It means you tried to subtract a future time from a past time (relatively) and got a negative time delta. These aren't intuitive in Python. You can do something like `str(abs(diff))` to get something human-readable. – Ouroborus Aug 21 '19 at 22:20
  • 1
    Possible duplicate of [Python timedelta issue with negative values](https://stackoverflow.com/questions/8408397/python-timedelta-issue-with-negative-values) – Ouroborus Aug 21 '19 at 22:20
  • You could also parse these with `fromisoformat` `delta = datetime.fromisoformat('2019-08-21T22:06:58') - datetime.fromisoformat('2019-08-21T22:05:59')` These would produce a positive delta because the earlier date is subtracted from the later date. – Chris Charley Aug 21 '19 at 22:26

0 Answers0