Not able to convert “00:30 AM” to 24 hours by strp

2019-06-14 02:33发布

问题:

I am trying to convert "00:30 AM" to 24 hours time but it gives ValueError: time data '00:30 AM' does not match format '%I:%M %p'

The code used is:

datetime.datetime.strptime('00:30 AM','%I:%M %p')

Can somebody please help me solving this error?

回答1:

The %I format expects a number between one and twelve, not zero and twelve. A 12 hour clock always uses 12 for midnight or noon.

From the strftime() documentation:

%I
Hour (12-hour clock) as a zero-padded decimal number.
01, 02, ..., 12

Either replace the 00 with 12 or use a different method of parsing.