Date Format Cheat Sheet · Tutorial
How to format dates in Python with strftime
5 min readbeginnerUpdated
You need a Python datetime printed as Wednesday, 4 March 2026 on a web page and as 2026-03-04T15:07:09+05:30 in an API. Using the cheat sheet, you'll pick the right strftime codes, avoid the codes that break on Windows, and copy ready-made formats.
What you need
- The Date Format Cheat Sheet with Python (strftime) selected
- A Python datetime object in your code
Steps
Open the Python table
Choose Python (strftime) under Language. The line at the top shows how a pattern is used.
dt.strftime("%Y-%m-%d")Build a readable date
Look up the weekday (
%A), day (%d, or%-dwithout a leading zero), month name (%B) and year (%Y). Together they printWednesday, 04 March 2026.dt.strftime("%A, %d %B %Y")Find a code quickly
Type
minuteunder Find a code to see that minutes are%M, while the month number is%m.Copy a common format
Under Common formats, copy the ISO 8601 pattern with offset,
%Y-%m-%dT%H:%M:%S%:z. The%:zcode needs Python 3.12; on older versions usedt.isoformat().
Try it yourself
Opens the Date Format Cheat Sheet with this tutorial's sample loaded.
Common mistakes
- %-d on Windows
- Codes with a dash come from the C library on Linux and macOS. On Windows use
%#d, or format the number yourself. - Empty %Z and %z
- A naive datetime has no time zone, so
%Zand%zprint nothing. Attach a zone withzoneinfofirst. - Expecting milliseconds from %f
%fprints 6 digits (microseconds). Cut the last three digits if you need milliseconds.
Next steps
Go back to the Date Format Cheat Sheet, or keep going with one of these: