Last updated: Apr 13, 2024
Reading time·3 min
You can use the %p
directive with the strftime()
method to format a
datetime
object as AM/PM.
The %p
directive displays the locale's equivalent of AM or PM.
from datetime import datetime # ✅ Format datetime as AM/PM dt_string = '2024-07-21 04:50 PM' dt_object = datetime.strptime(dt_string, '%Y-%m-%d %H:%M %p') print(dt_object) print(dt_object.strftime('%Y-%m-%d %H:%M %p'))
Running the code sample produces the following output.
2024-07-21 04:50:00 2024-07-21 04:50 AM
You can view all of the directives for the strftime() method in this table in the docs.
For example, your date string might also include the seconds.
from datetime import datetime dt_string = '2024-07-21 04:50:34 PM' dt_object = datetime.strptime(dt_string, '%Y-%m-%d %H:%M:%S %p') print(dt_object) print(dt_object.strftime('%Y-%m-%d %H:%M:%S %p'))
Running the code sample produces the following output.
2024-07-21 04:50:34 2024-07-21 04:50:34 AM
Let's go over the directives used in the format string:
%Y
- the year with century as a decimal number.%m
- the month as a zero-padded decimal number.%d
- the day of the month as a zero-padded decimal number.%H
- the hour as a zero-padded (24-hour clock) decimal number.%M
- the minutes as a zero-padded decimal number.%S
- the seconds as a zero-padded decimal number.%p
- the locale's equivalent of either AM or PM.Note that there is also a %I
directive that formats the hour (12-hour clock)
as a zero-padded decimal number.
from datetime import datetime dt_string = '2024-07-21 04:50:34 PM' dt_object = datetime.strptime(dt_string, '%Y-%m-%d %I:%M:%S %p') print(dt_object) print(dt_object.strftime('%Y-%m-%d %I:%M:%S %p'))
Running the code sample produces the following output.
2024-07-21 16:50:34 2024-07-21 04:50:34 PM
You can use the same approach if you only need to format the time as AM/PM.
from datetime import datetime # ✅ without seconds print(datetime.today().strftime('%I:%M %p')) # ✅ with seconds print(datetime.today().strftime('%I:%M:%S %p'))
Running the code sample produces the following output.
09:12 AM 09:12:07 AM
The %I
directive formats the hour as a 12-hour clock zero-padded decimal
number.
You can also use the %H
directive to format the hour as a 24-hour clock
zero-padded decimal number.
from datetime import datetime print(datetime.today().strftime('%H:%M %p')) print(datetime.today().strftime('%H:%M:%S %p'))
This approach also works if you need to convert a time string to a datetime
object and format it as AM/PM.
from datetime import datetime time_string = '10:50:23 PM' dt_object = datetime.strptime(time_string, '%I:%M:%S %p') print(dt_object) print(dt_object.strftime('%I:%M:%S %p'))
Running the code sample produces the following output.
1900-01-01 22:50:23 10:50:23 PM
We could've also used %H
for 24-hour clock zero-padded hours.
from datetime import datetime time_string = '10:50:23 PM' dt_object = datetime.strptime(time_string, '%H:%M:%S %p') print(dt_object) print(dt_object.strftime('%H:%M:%S %p'))
Running the code sample produces the following output.
1900-01-01 10:50:23 10:50:23 AM
In some cases, you might need to use the
str.replace method to ensure
your datetime
string is formatted correctly to avoid getting the
ValueError: time data 'X' does not match format '%Y-%m-%d'.
Here is an example.
from datetime import datetime dt_string = '2024-07-21 04:50 p.m.' # ⛔️ Error dt_object = datetime.strptime(dt_string, '%Y-%m-%d %H:%M %p') print(dt_object) print(dt_object.strftime('%Y-%m-%d %H:%M %p'))
Notice that the datetime
string ends with p.m.
instead of AM
or PM
.
Therefore, the %p
directive cannot parse the string.
To resolve the issue in this case, use the str.replace()
method to replace the
p.m.
part of the string with PM
.
from datetime import datetime dt_string = '2024-07-21 04:50 p.m.' dt_string = dt_string.replace('p.m.', 'PM') dt_object = datetime.strptime(dt_string, '%Y-%m-%d %H:%M %p') print(dt_object) print(dt_object.strftime('%Y-%m-%d %H:%M %p'))
Running the code sample produces the following output.
2024-07-21 04:50:00 2024-07-21 04:50 AM
You can learn more about the related topics by checking out the following tutorials: