Editor

Python strftime tester

Format and read dates with Python's own datetime, and see the result as you type.

Python strftime format codes

Examples for Thursday 5 March 2026, 14:04:09.123456, in IST. Select a code to add it to the tester.

CodeMeaningExample
Date
Day of the month, 2 digits05
Day of the month, no leading zero (Linux and macOS; %#d on Windows)5
Month as a number, 2 digits03
Month as a number, no leading zero3
Month name, shortMar
Month name, fullMarch
Year, 2 digits26
Year, 4 digits2026
Weekday, shortThu
Weekday, fullThursday
Weekday as a number, Sunday is 04
Weekday as a number, Monday is 1 (ISO)4
Day of the year, 3 digits064
Time
Hour, 24-hour clock, 2 digits14
Hour, 12-hour clock, 2 digits02
Hour, 12-hour clock, no leading zero2
AM or PMPM
Minute, 2 digits04
Second, 2 digits09
Microsecond, 6 digits123456
Time zone
UTC offset (empty for a date without a time zone)+0530
UTC offset with a colon (Python 3.12 and later)+05:30
Time zone name (empty without a time zone)IST
Weeks
Week of the year, weeks start on Sunday09
Week of the year, weeks start on Monday09
ISO week number10
ISO year (goes with %V)2026
Whole dates and other
Date and time in the locale's formatThu Mar 5 14:04:09 2026
Date in the locale's format03/05/26
Time in the locale's format14:04:09
A literal % sign%

Common Python date formats

FormatCodeExample
ISO 8601 date%Y-%m-%d2026-03-05
Day/month/year%d/%m/%Y05/03/2026
Month/day/year (US)%m/%d/%Y03/05/2026
No leading zeros%-d/%-m/%Y5/3/2026
Long date%A, %d %B %YThursday, 05 March 2026
Month and day%b %-dMar 5
12-hour time%I:%M %p02:04 PM
Timestamp%Y-%m-%d %H:%M:%S2026-03-05 14:04:09
For a file name%Y%m%d_%H%M%S20260305_140409
Email and HTTP date%a, %d %b %Y %H:%M:%S %zThu, 05 Mar 2026 14:04:09 +0530
ISO week date%G-W%V-%u2026-W10-4

More on dates in the Python datetime lesson, and two programs to run: print the current date and time and days between two dates.

Questions

What is the difference between strftime and strptime?

strftime turns a datetime into text ("f" for format), and strptime reads text into a datetime ("p" for parse). Both use the same format codes: d.strftime("%d/%m/%Y") gives "05/03/2026", and datetime.strptime("05/03/2026", "%d/%m/%Y") gives the date back.

How do I remove the leading zero from the day or month?

On Linux and macOS (and in this tester) write %-d and %-m. Windows uses %#d and %#m instead. To work everywhere, use the attributes: f"{d.day}/{d.month}/{d.year}".

How do I show milliseconds?

Python has no code for milliseconds. %f gives 6 digits of microseconds, so cut the last three: d.strftime("%H:%M:%S.%f")[:-3] gives "14:04:09.123".

Why do %z and %Z print nothing?

The datetime has no time zone (it is "naive"). Give it one, for example datetime.now(timezone.utc), or d.replace(tzinfo=...), and %z prints the offset.

What does "unconverted data remains" mean in strptime?

The text has more characters than the format describes, for example a time after the date. Add the missing codes to the format, or cut the text to the part you need.

Is there a shortcut for ISO 8601?

Yes: d.isoformat() gives "2026-03-05T14:04:09.123456+05:30", and datetime.fromisoformat() reads that format back, faster than strptime.