How can I account for period AM/PM using strftime?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To show AM or PM in Python time formatting, use %p together with the 12-hour clock token %I. The mistake people usually make is pairing %p with the 24-hour token %H, which produces odd output such as 15:45 PM. Once you separate 12-hour and 24-hour formatting clearly, the behavior is predictable.
Use %I and %p Together
The normal 12-hour format is %I:%M %p.
That prints 03:45 PM. The pieces mean:
- '
%Ifor the hour on a 12-hour clock' - '
%Mfor minutes' - '
%pfor the locale-specific period marker'
If you want seconds too, extend the same pattern:
Do Not Mix %H with %p
A common mistake is writing this:
%H is the 24-hour clock token, so the output can look like 15:45 PM. That is syntactically allowed, but it is not the display format most people want.
The rule is simple:
- use
%Hfor 24-hour time with noAMorPM - use
%Iwith%pfor 12-hour time
Midnight and Noon Are the Important Edge Cases
People often hesitate around midnight and noon because 12-hour clocks represent them differently from 24-hour clocks.
The results are:
- midnight becomes
12:00 AM - noon becomes
12:00 PM
That is correct for 12-hour display, even though the underlying hour values are 0 and 12 in the datetime object.
Parsing AM and PM Uses strptime
Formatting is only one side of the problem. If you also need to read an input string such as 03:45 PM, use strptime with the same 12-hour pattern.
The parsed hour becomes 15. Matching the format string for both directions keeps the code easier to reason about.
A useful rule is this: if you format with %I:%M %p, parse with %I:%M %p. If you later change the display format, update both sides together.
%p Depends on Locale
%p is locale-sensitive. In many English-language environments it produces AM and PM, but other locales may use different text. That is helpful for localized output, but it also means you should not assume the exact string is always English.
If you specifically want lowercase am and pm, format first and then transform the result.
That keeps the clock logic in strftime and the stylistic choice as a separate presentation step.
Common Pitfalls
- Using
%Hwith%pand producing a confusing 24-hour-plus-period format. - Forgetting that
%Iis the 12-hour token and%His the 24-hour token. - Misreading midnight and noon when checking formatted output.
- Parsing an
AMorPMtime string with%Hinstead of%I. - Assuming
%pwill always render as literal EnglishAMandPMin every locale.
Summary
- Use
%Iwith%pto format 12-hour times that includeAMorPM. - Use
%Honly for 24-hour formatting. - Midnight formats as
12:00 AM, and noon formats as12:00 PM. - Use the same
%Iand%ppattern withstrptimewhen parsing input. - Remember that
%pis locale-aware and may vary by environment.

