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.
| Code | Meaning | Example |
|---|---|---|
| Date | ||
| Day of the month, 2 digits | 05 | |
| Day of the month, no leading zero (Linux and macOS; %#d on Windows) | 5 | |
| Month as a number, 2 digits | 03 | |
| Month as a number, no leading zero | 3 | |
| Month name, short | Mar | |
| Month name, full | March | |
| Year, 2 digits | 26 | |
| Year, 4 digits | 2026 | |
| Weekday, short | Thu | |
| Weekday, full | Thursday | |
| Weekday as a number, Sunday is 0 | 4 | |
| Weekday as a number, Monday is 1 (ISO) | 4 | |
| Day of the year, 3 digits | 064 | |
| Time | ||
| Hour, 24-hour clock, 2 digits | 14 | |
| Hour, 12-hour clock, 2 digits | 02 | |
| Hour, 12-hour clock, no leading zero | 2 | |
| AM or PM | PM | |
| Minute, 2 digits | 04 | |
| Second, 2 digits | 09 | |
| Microsecond, 6 digits | 123456 | |
| 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 Sunday | 09 | |
| Week of the year, weeks start on Monday | 09 | |
| ISO week number | 10 | |
| ISO year (goes with %V) | 2026 | |
| Whole dates and other | ||
| Date and time in the locale's format | Thu Mar 5 14:04:09 2026 | |
| Date in the locale's format | 03/05/26 | |
| Time in the locale's format | 14:04:09 | |
| A literal % sign | % | |
Common Python date formats
| Format | Code | Example |
|---|---|---|
| ISO 8601 date | %Y-%m-%d | 2026-03-05 |
| Day/month/year | %d/%m/%Y | 05/03/2026 |
| Month/day/year (US) | %m/%d/%Y | 03/05/2026 |
| No leading zeros | %-d/%-m/%Y | 5/3/2026 |
| Long date | %A, %d %B %Y | Thursday, 05 March 2026 |
| Month and day | %b %-d | Mar 5 |
| 12-hour time | %I:%M %p | 02:04 PM |
| Timestamp | %Y-%m-%d %H:%M:%S | 2026-03-05 14:04:09 |
| For a file name | %Y%m%d_%H%M%S | 20260305_140409 |
| Email and HTTP date | %a, %d %b %Y %H:%M:%S %z | Thu, 05 Mar 2026 14:04:09 +0530 |
| ISO week date | %G-W%V-%u | 2026-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.