How do you format a string in the format of
HH:MM AM/PM
to 24-hour time in Python?
To change a string in the format
HH:MM AM/PM
to
24-hour time in Python
use the
datetime.datetime.strptime(original_string, format_string)
function located in the
datetime
library.
As the
datetime
class needs to be imported from the
datetime
library you need to make sure you include the reference twice, otherwise you may get
strptime
AttributeError
errors
.
Here’s an example of how this works:
>>> from datetime import datetime
>>> t = "1:58PM"
>>> datetime.strptime(t, "%I:%M%p")
datetime.datetime(1900, 1, 1, 13, 12)
As you can see from the result the
datetime
object contains the year, month, day of the month,
hour in 24-hour format
, and minute. As the original string time was
1:58PM
this when converted to a 24-hour clock produces the time
13:12
.
Therefore, to produce this result you use a couple of means to produce a string representing the original time in 24-hours, like so:
>>> from datetime import datetime
>>> t = "1:58PM"
>>> time_24hrs = datetime.strptime(t, "%I:%M%p")
>>> time_24hrs.strftime("%H:%M")
'13:58'
>>> f"{time_24hrs:%H:%M}"
'13:58'
As you can see from the above output the two methods of outputting the original time in 24-hour format can be performed using the built-in string method
.strftime(format_string)
or by using an f-string and a modifier in the f-string to output the intended result.
The format code needed to output a time in 24-hours is
%H:%M
.
ValueError
Using
strptime
On Time
There are a couple of instances where the conversion may fail depending on whether you’ve correctly inserted a proper time and/or whether the format string matches the input.
Here are some examples where you may get a
ValueError
result when trying to convert your time string:
>>> from datetime import datetime
>>> t = "1:58 PM"
>>> datetime.strptime(t, "%I:%M%p")
ValueError: time data '1:58 PM' does not match format '%I:%M%p'
In this example, the reason for the
ValueError
was due to a simple missing space between the minute and the
AM/PM
designation. To fix this issue simply modify the format string to
%I:%M %p
(notice the space between the minute
%M
and the AM/PM identifier
%p
.
>>> from datetime import datetime
>>> t = "13:58"
>>> datetime.strptime(t, "%I:%M")
ValueError: time data '1:68PM' does not match format '%I:%M"
The above
ValueError
occurs because the original string contained
24-hour time
, but the format string is expecting the hour to be in 12-hour time intervals. The simple fix needed here would be to swap the
%I
with
%H
.
Finally, do be careful of the hour identifier
%H
and
%I
. It can be easy when not using this often to designate the hour as
%H
, but this should only be done when the time string is originally in 24-hour format.
Notice the error here:
>>> from datetime import datetime
>>> t = "1:58PM"
>>> datetime.strptime(t, "%H:%M%p")
datetime.datetime(1900, 1, 1, 1, 58)
As you can see the hour parameter in the
datetime
object is
1
not
13
. Therefore, the conversion is incorrect. You need to replace the
%H
with the proper
%I
syntax.
Summary
In this article I discuss how to convert a string in the format
HH:MM AM/PM
to
24-hour time in Python
. The process involves using the
datetime.datetime.strptime()
function to convert the string to a
datetime
object, and then using the string method
strftime()
to output the time in 24-hour format.
The format code for 24-hour time is
%H:%M
whereas the format for 12-hour time is
%I:%M
with
%p
appended if the time contains AM/PM. There are a couple of potential errors that can occur when converting strings, namely due to incorrect formatting or incorrect insertion of time values.