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

Steps

  1. 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")
  2. Build a readable date

    Look up the weekday (%A), day (%d, or %-d without a leading zero), month name (%B) and year (%Y). Together they print Wednesday, 04 March 2026.

    dt.strftime("%A, %d %B %Y")
  3. Find a code quickly

    Type minute under Find a code to see that minutes are %M, while the month number is %m.

  4. Copy a common format

    Under Common formats, copy the ISO 8601 pattern with offset, %Y-%m-%dT%H:%M:%S%:z. The %:z code needs Python 3.12; on older versions use dt.isoformat().

Try it yourself

Opens the Date Format Cheat Sheet with this tutorial's sample loaded.

Try it now

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 %Z and %z print nothing. Attach a zone with zoneinfo first.
Expecting milliseconds from %f
%f prints 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: