Python datetime Format Codes

All strftime/strptime format codes with example output and locale notes.

Searchable reference for Python datetime strftime and strptime format directives — %Y, %m, %d, %H, %M, %S, %j, %a, %B and more — each with meaning, example output, and locale notes. Build a format string live and preview the result.

What is the difference between strftime and strptime?

strftime formats a datetime object into a string using directives like %Y. strptime parses a string back into a datetime using the same directives as the expected layout. The directive set is shared between them.

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.

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.
  • %f always produces six digits (microseconds), padded with zeros.
  • ISO week directives %G, %V, %u form a consistent ISO 8601 triple and should be used together, never mixed with %Y, %U, or %W.
  • Names produced by %a, %A, %b, %B, and %p follow the active locale.