Skip to content
Advertisement

python datetime strptime format

I have a datetime string like:

"2016-08-15T07:50:12"

I used strptime() function in datetime module to convert the string to datetime object. My datetime format is

"%Y-%m-%dT%H:%M:%S.%f"

When I parse this above string, the function raises ValueError because of missing millisecond part in the string. How can I have datetime object with millisecond is 0 when I don’t specify it in the string?

Advertisement

Answer

If you get an ISO 8601 string like: “2016-08-15T07:50:12” easiest way I feel is using dateutil to convert it.

import dateutil.parser
yourdate = dateutil.parser.parse(datestring)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement