Python’s datetime.strftime and datetime.strptime share one set of format
directives, each beginning with %. Get one wrong and your parse silently
fails or your output is off by a century. This tool is a searchable reference
plus a live builder that renders any format string against a sample timestamp.
How it works
Each directive maps a part of a date or time to a piece of text. strftime
walks your format string left to right and substitutes each %X with the
corresponding field of the datetime; everything else is copied verbatim.
strptime does the reverse, matching the input text against the directives.
The reference covers the standard C89 directives that CPython supports on every
platform — year (%Y, %y), month (%m, %b, %B), day (%d, %j),
weekday (%a, %A, %w, %u), time (%H, %I, %M, %S, %p),
microseconds (%f), zone (%z, %Z), week numbers (%U, %W, %V, %G),
and the locale shortcuts (%c, %x, %X). A literal percent sign is %%.
Worked example
For the timestamp 2026-06-11 14:05:09:
%Y-%m-%d -> 2026-06-11
%d/%m/%Y -> 11/06/2026
%A, %d %B %Y -> Thursday, 11 June 2026
%I:%M %p -> 02:05 PM
%j (day of yr) -> 162
The builder above renders your own string the same way so you can confirm the layout before pasting it into code.
Quick-reference table of the most-used directives
| Directive | Meaning | Example (2026-06-11 14:05:09) |
|---|---|---|
%Y | 4-digit year | 2026 |
%y | 2-digit year | 26 |
%m | Month as zero-padded number | 06 |
%B | Full month name | June |
%b | Abbreviated month name | Jun |
%d | Day of month, zero-padded | 11 |
%A | Full weekday name | Thursday |
%a | Abbreviated weekday | Thu |
%H | Hour (24-hour), zero-padded | 14 |
%I | Hour (12-hour), zero-padded | 02 |
%M | Minute, zero-padded | 05 |
%S | Second, zero-padded | 09 |
%p | AM or PM | PM |
%f | Microseconds, 6 digits | 000000 |
%j | Day of year (001–366) | 162 |
%z | UTC offset (+HHMM) | +0000 or empty |
%Z | Time zone name | UTC or empty |
%% | Literal percent sign | % |
ISO week numbers: use %G, %V, %u together
The ISO 8601 week date system treats Monday as the first day of the week and assigns a week to the year in which its Thursday falls. This means the first few days of January can belong to week 52 or 53 of the previous year, and the last few days of December can belong to week 1 of the next year.
%Gis the ISO year (can differ from%Yin those edge days)%Vis the ISO week number (01–53)%uis the ISO weekday (1=Monday … 7=Sunday)
Always use these three together. Mixing %G with %m or %V with %Y produces inconsistent dates at year boundaries.
Notes
- Zero-padding is the default for numeric directives. Platform extensions like
%-d(no leading zero) exist on Linux and macOS but are not portable to Windows, so this reference treats them as non-standard. %falways produces six digits (microseconds), padded with zeros.- Names produced by
%a,%A,%b,%B, and%pfollow the active locale — setlocale.setlocale(locale.LC_TIME, ...)to get non-English names. - For timezone-aware datetimes, attach a
tzinfoobject before callingstrftime; without it%zand%Zboth produce empty strings.