1

My timestanp string is as follows

mytime = 2021-10-25T14:17:13+0000

As you can see the timezone is shown as 0000 instead of 00:00. This seems to cause an error when I do

newtime = datetime.fromisoformat(mytime)

Invalid isoformat string: '2021-10-25T14:17:13+0000'

How should I proceed to convert this string into a datetime object?

Taku
  • 31,927
  • 11
  • 74
  • 85
pepe
  • 9,799
  • 25
  • 110
  • 188
  • What format is that timestamp and where are you getting it from? – Skully Oct 25 '21 at 23:57
  • 1
    `datetime.fromisoformat()` is only meant to parse time strings created by `datetime.isoformat()`, which accepts only a subset of ISO-8601 formats. You'll need to use some other method in the linked post. – Taku Oct 26 '21 at 00:01
  • yes I checked that post, and when I use `datetime.strptime(mytime), '%Y-%m-%dT%H:%M:%S+%z')` I get `time data '2021-10-25T14:17:13+0000' does not match format '%Y-%m-%dT%H:%M:%S+%z'`—any ideas? – pepe Oct 26 '21 at 00:08
  • 2
    You'll need to remove the plus sign in "+%z" since the plus sign is already part of %z – Taku Oct 26 '21 at 00:11
  • worth to mention I think that dateutil's isoparser seems to be more efficient than datetime's strptime (e.g. `parser.isoparse('2021-10-25T14:17:13+0000')` is nearly 2x faster in `%timeit` on my machine. – FObersteiner Oct 26 '21 at 06:07

0 Answers0