0

I am trying to find a time diff between two timestamp.

Code:

fmt = '%Y-%m-%d %H:%M:%S:%fZ'
tstamp1 = datetime.strptime(first_timestamp, fmt)
tstamp2 = datetime.strptime(second_timestamp, fmt)
td = tstamp1 - tstamp2
td_mins = int(round(td.total_seconds() / 60))
total_min = total_min + td_mins

But I am getting error as "time data '2022-04-20T05:42:05Z' does not match format '%Y-%m-%d*%H:%M:%S:%fZ'"

My timestamp are in format 2022-04-20T05:42:05Z - which is ISO 8601.

How to deal with this?

D kashyap
  • 25
  • 7
  • 1
    Does this answer your question? [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – PM 77-1 Aug 19 '22 at 14:09
  • Your parsing directive is wrong. It does not match the input: your input has no fractional seconds (%f). Also, use %z to parse Z to UTC (instead of a literal Z in the parsing directive). – FObersteiner Aug 19 '22 at 15:23
  • I converted the T and Z from the timestamp using convert = {"T" : " " , "Z": ""} and now I am able to use it as fmt = '%Y-%m-%d %H:%M:%S' – D kashyap Aug 22 '22 at 14:17

0 Answers0