0
import datetime as dt

time_str = '2022-02-25 18:37:46.594385+00:00'

Then I try to convert this into a datetime object as follows:

dt.datetime.strptime(time_str,'%Y-%m-%d %H:%M:%S.%f%z')

However it results in the following error:

ValueError: time data '2022-02-25 18:37:46.594385+00:00' does not match format '%Y-%m-%d %H:%M:%S.%f%z'

The error appears to be coming from the %z section of the format, but overall I do not understand why this is not working, since it seems the specified format does match the string format. If you could help identify any issues and suggest a solution to convert the example time_str into a datetime object successfully. Thanks!

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
agftrading
  • 784
  • 3
  • 8
  • 21
  • which Python version are you using? Note the [technical details](https://docs.python.org/3/library/datetime.html#technical-detail): `%z` can only parse colon-separated UTC offset (hh:mm) with version 3.7 or higher. – FObersteiner Feb 25 '22 at 19:35
  • see [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/q/127803/10197418) for other options. – FObersteiner Feb 25 '22 at 19:37
  • Python 3.6.3. So ok, understood. – agftrading Feb 25 '22 at 19:48

0 Answers0