strftime is a Python datetime method used to convert date and time objects into formatted strings. It works by applying format codes like %Y, %m, and %d to control how dates and times are displayed in logs, filenames, APIs, and user interfaces.
Working with dates in Python almost always comes down to formatting. You either need to display a date for users, generate timestamps for logs, or match a specific format required by an API. That’s where strftime shows up.
This guide is a practical strftime cheat sheet for Python developers. It includes a complete table of format codes, real examples you actually use, a way to test formats interactively, and a clear comparison with strptime so you don’t mix them up.
No theory dump. Just what you need.
What Is strftime in Python?
strftime stands for string format time. It converts a datetime, date, or time object into a formatted string using format specifiers.
Basic example:
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)
Output:
2025-12-24 17:30:45
You control the output using format codes, each starting with %.
Complete strftime Format Codes Cheat Sheet
This table covers the most commonly used and officially supported strftime directives in Python.
Date Components
| Code | Meaning | Example |
|---|---|---|
%Y | Year (4 digits) | 2025 |
%y | Year (2 digits) | 25 |
%m | Month (01–12) | 12 |
%B | Full month name | December |
%b | Short month name | Dec |
%d | Day of month (01–31) | 24 |
%j | Day of year (001–366) | 358 |
%A | Full weekday name | Wednesday |
%a | Short weekday name | Wed |
%w | Weekday (0–6, Sunday=0) | 3 |
Time Components
| Code | Meaning | Example |
|---|---|---|
%H | Hour (24-hour clock) | 17 |
%I | Hour (12-hour clock) | 05 |
%p | AM or PM | PM |
%M | Minute | 30 |
%S | Second | 45 |
%f | Microseconds | 123456 |
Week & Locale
| Code | Meaning | Example |
|---|---|---|
%U | Week number (Sunday start) | 51 |
%W | Week number (Monday start) | 52 |
%c | Locale date & time | Wed Dec 24 17:30:45 2025 |
%x | Locale date | 12/24/25 |
%X | Locale time | 17:30:45 |
Timezone
| Code | Meaning | Example |
|---|---|---|
%z | UTC offset | +0530 |
%Z | Timezone name | IST |
Literal Percent
| Code | Meaning |
|---|---|
%% | Literal % |
Common strftime Examples You’ll Actually Use
ISO 8601 Date
"%Y-%m-%d"
Output:
2025-12-24
ISO DateTime (API Friendly)
"%Y-%m-%dT%H:%M:%S"
Output:
2025-12-24T17:30:45
Human-Readable Date
"%B %d, %Y"
Output:
December 24, 2025
Log Timestamp
"%Y-%m-%d %H:%M:%S"
Output:
2025-12-24 17:30:45
12-Hour Clock with AM/PM
"%I:%M %p"
Output:
05:30 PM
Filename-Safe Timestamp
"%Y%m%d_%H%M%S"
Output:
20251224_173045
Useful for backups, exports, and reports.
Interactive strftime Format Tester
When working with complex formats, memorizing codes slows you down. The faster approach is testing formats interactively.
On this page, you can use the interactive strftime testing tool to:
- Enter a format string
- See output instantly
- Adjust until it matches your requirement
This is especially helpful when:
- Matching third-party API date formats
- Generating reports
- Formatting timestamps for logs or filenames
Instead of running Python repeatedly, you validate formats in one place.
strftime vs strptime (Important Difference)
These two are often confused.
strftime → datetime ➜ string
You format a date.
now.strftime("%Y-%m-%d")
strptime → string ➜ datetime
You parse a date.
from datetime import datetime
dt = datetime.strptime("2025-12-24", "%Y-%m-%d")
Side-by-Side Comparison
| Function | Direction | Purpose |
|---|---|---|
strftime | datetime → string | Display or export dates |
strptime | string → datetime | Parse input strings |
Same format codes. Opposite direction.
This is the key thing to remember.
Common strftime Mistakes (Seen in Real Projects)
Using %m Instead of %M
%m= month%M= minute
This causes subtle bugs that are hard to notice.
Forgetting Zero Padding
%dgives01%-d(Linux only) gives1
Be careful when targeting cross-platform code.
Timezone Assumptions
strftime formats whatever timezone your datetime object already has. If you’re working with APIs, always confirm whether the datetime is:
- naive
- UTC
- localized
Best Practices for Python Date Formatting
- Prefer ISO formats for APIs
- Use human-readable formats only for UI
- Keep timestamps timezone-aware
- Test formats using an interactive tool instead of guessing
- Document expected formats clearly in your codebase
When You Should Use strftime
Use strftime when:
- Displaying dates to users
- Creating log entries
- Generating filenames
- Exporting CSV or reports
- Matching external format requirements
Avoid using it for date math. That’s what datetime operations are for.
FAQs About Python strftime
What does strftime stand for?
It stands for string format time — converting date/time objects into strings.
Is strftime locale-dependent?
Yes. Some format codes depend on system locale, especially month and weekday names.
Does strftime handle timezones?
It formats whatever timezone the datetime object already has. It does not convert timezones by itself.
Is strftime consistent across operating systems?
Mostly, but some formatting flags behave differently. Stick to standard directives for portability.
Final Thoughts
strftime is simple, but mistakes around formatting can break APIs, logs, and reports silently. Having a reliable cheat sheet and a way to test formats instantly saves time and prevents bugs.
This page is designed to be:
- Quick to reference
- Accurate
- Practical for real Python work
Bookmark it. You’ll come back.
