0

I get an error saying that the format is different from the ISO format and I can't seem to convert it to date type. What should I do?

import datetime
timestamp1='2022-03-03T14:13:52.847000+00:00'
timestamp2='2022-03-03T12:50:26.785000+00:00'

time = datetime.date.fromisoformat(timestamp1) - datetime.date.fromisoformat(timestamp2)
print(time)

ValueError: Invalid isoformat string: '2022-03-03T14:13:52.847000+00:00'
  • Well, this isn't ISO format. Maybe you should use [`datetime.datetime.strptime`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime) instead – aaossa Mar 03 '22 at 14:42
  • 2
    @aaossa the problem is not with ISO format, the problem is that OP is using `date.fromisoformat` - but the input contains more than a date, so use `datetime.fromisoformat`. – FObersteiner Mar 03 '22 at 16:23

1 Answers1

0

You could split the string and only use the part that contains a date:

datetime.date.fromisoformat(timestamp1.split("T")[0])
aaossa
  • 3,763
  • 2
  • 21
  • 34