Search code examples
pythondatetimeiso8601

Correct Python datetime FMT


what is wrong with my FMT formatting for datetime? My date is formatted as follows:

mytime = '2021-12-06T13:52:41.864+0000'

I am trying to parse it with

FMT = '%Y-%m-%dT%H:%M:%S.%f+%Z'

and

FMT = '%Y-%m-%dT%H:%M:%S.%f+0000'

To be able to do:

datetime.strptime(mytime, FMT)

Both my solutions do not work. Any idea?


Solution

  • remove + and use z instead of Z.

    from datetime import datetime
    
    mytime = '2021-12-06T13:52:41.864+0000'
    datetime.strptime(mytime, '%Y-%m-%dT%H:%M:%S.%f%z')
    

    output:

    datetime.datetime(2021, 12, 6, 13, 52, 41, 864000, tzinfo=datetime.timezone.utc)