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. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

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.

Quick-reference table of the most-used directives

DirectiveMeaningExample (2026-06-11 14:05:09)
%Y4-digit year2026
%y2-digit year26
%mMonth as zero-padded number06
%BFull month nameJune
%bAbbreviated month nameJun
%dDay of month, zero-padded11
%AFull weekday nameThursday
%aAbbreviated weekdayThu
%HHour (24-hour), zero-padded14
%IHour (12-hour), zero-padded02
%MMinute, zero-padded05
%SSecond, zero-padded09
%pAM or PMPM
%fMicroseconds, 6 digits000000
%jDay of year (001–366)162
%zUTC offset (+HHMM)+0000 or empty
%ZTime zone nameUTC 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.

  • %G is the ISO year (can differ from %Y in those edge days)
  • %V is the ISO week number (01–53)
  • %u is 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.
  • %f always produces six digits (microseconds), padded with zeros.
  • Names produced by %a, %A, %b, %B, and %p follow the active locale — set locale.setlocale(locale.LC_TIME, ...) to get non-English names.
  • For timezone-aware datetimes, attach a tzinfo object before calling strftime; without it %z and %Z both produce empty strings.