0

i am using datetime.strptime() in order to convert the timesatmp string received from the API to separate the date and time. one of the example :

from datetime import 
datetime.strptime("2019-11-14T03:41:12.869000Z","%Y-%m-%dT%H:%M:%S.%f%

and I can't figure out what is wrong with this "2019-11-14T03:41:12.869000Z","%Y-%m-%dT%H:%M:%S.%f%Z" ?

Rachit
  • 43
  • 1
  • 6
  • 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) – FObersteiner Jun 29 '22 at 09:00

2 Answers2

0

%Z is Time zone name (empty string if the object is naive). for example UTC. If trailing Z appears in all your strings use Z rather than %Z, that is

import datetime
dt = datetime.datetime.strptime("2019-11-14T03:41:12.869000Z","%Y-%m-%dT%H:%M:%S.%fZ")
print(dt)

output

2019-11-14 03:41:12.869000
Daweo
  • 31,313
  • 3
  • 12
  • 25
  • If you ignore the Z by passing a literal Z, you end up with naive datetime. That can be pretty misleading if you expect UTC since Python treats naive datetime as local time. – FObersteiner Jun 29 '22 at 09:02
0

Just remove the % before Z and it should work!

from datetime import datetime
datetime.strptime("2019-11-14T03:41:12.869000Z","%Y-%m-%dT%H:%M:%S.%fZ")
Pythonista
  • 185
  • 12