How to format datetime or time as AM/PM in Python

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. How to format datetime or time as AM/PM in Python
  2. Format time as AM/PM in Python
  3. Making sure your datetime string is formatted correctly

# How to format datetime or time as AM/PM in Python

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.

main.py
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'))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
2024-07-21 04:50:00 2024-07-21 04:50 AM

python format datetime am pm

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.

main.py
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'))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
2024-07-21 04:50:34 2024-07-21 04:50:34 AM

also including the seconds

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.

main.py
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.

shell
2024-07-21 16:50:34 2024-07-21 04:50:34 PM

# Format time as AM/PM in Python

You can use the same approach if you only need to format the time as AM/PM.

main.py
from datetime import datetime # ✅ without seconds print(datetime.today().strftime('%I:%M %p')) # ✅ with seconds print(datetime.today().strftime('%I:%M:%S %p'))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
09:12 AM 09:12:07 AM

format time as am pm in python

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.

main.py
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.

main.py
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.

shell
1900-01-01 22:50:23 10:50:23 PM

convert time string to datetime object format as am pm

We could've also used %H for 24-hour clock zero-padded hours.

main.py
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.

shell
1900-01-01 10:50:23 10:50:23 AM

using h for 24 hour zero padded hours

# Making sure your datetime string is formatted correctly

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.

main.py
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'))

avoid getting value error

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.

main.py
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'))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
2024-07-21 04:50:00 2024-07-21 04:50 AM

making sure am pm is spelled correctly

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.