Search code examples
pythonpython-2.7timestrptimepython-datetime

Not able to convert "00:30 AM" to 24 hours by strptime python


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?


Solution

  • 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.