Home > other >  Correct Python datetime FMT
Correct Python datetime FMT

Time:03-17

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?

CodePudding user response:

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)

CodePudding user response:

You're using the wrong timezone directive in your FMT, use %z, not %Z.

%z: UTC offset in the form HHMM or -HHMM.

  • Related