The full table of directives, with examples for each. Both strftime (format a datetime as a string) and strptime (parse a string into a datetime) use the same set of codes.
Examples are all formatted from this datetime: datetime(2026, 4, 21, 14, 30, 5) — a Tuesday, day 112 of the year.
%z in strftime is reliable; parsing with %z in strptime depends on the exact format. %Z parsing is unreliable across platforms — prefer fromisoformat for zone-aware strings.
dt=datetime(2026,4,21,14,30,5)dt.strftime("%Y-%m-%d")# 2026-04-21 (ISO date)dt.strftime("%Y-%m-%dT%H:%M:%S")# 2026-04-21T14:30:05 (ISO datetime)dt.strftime("%d/%m/%Y")# 21/04/2026 (British)dt.strftime("%m/%d/%Y")# 04/21/2026 (American)dt.strftime("%A %d %B %Y")# Tuesday 21 April 2026dt.strftime("%d %b %Y, %H:%M")# 21 Apr 2026, 14:30dt.strftime("%Y-W%V-%u")# 2026-W17-2 (ISO week-date)dt.strftime("%Y%m%d")# 20260421 (sortable, filename-safe)dt.strftime("%Y%m%dT%H%M%S")# 20260421T143005 (basic ISO, filename-safe)
strftime is the same in every direction — whatever you put in the format string, that's what you get back. The inverse for parsing is strptime(string, format) using the same directives.