Input format to convert with datetime.strptime

Viewed 255

I am trying to convert a string with "2019-01-06T01:00:24.908821" to a date using the "datetime.strptime" function. However, I am not able to find the format for this conversion to be successful.

I'm using the entries as proposed by the library itself, however I'm getting a "ValueError" when I try to perform the conversion.

ValueError: time data '2019-01-06T01:00:24.908821' does not match format '%Y-%m-%dT%H:%M:%S:%f'

If you would like to read the proposed standards, you can find here: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

My code:

from datetime import datetime

datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S:%f")
1 Answers

You have a colon (:) instead of a decimal (.) before the %f in your format string.

Change

datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S:%f")

To

datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S.%f")
Related