strftime Cheat Sheet 2025: Complete Python Developer Guide

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

CodeMeaningExample
%YYear (4 digits)2025
%yYear (2 digits)25
%mMonth (01–12)12
%BFull month nameDecember
%bShort month nameDec
%dDay of month (01–31)24
%jDay of year (001–366)358
%AFull weekday nameWednesday
%aShort weekday nameWed
%wWeekday (0–6, Sunday=0)3

Time Components

CodeMeaningExample
%HHour (24-hour clock)17
%IHour (12-hour clock)05
%pAM or PMPM
%MMinute30
%SSecond45
%fMicroseconds123456

Week & Locale

CodeMeaningExample
%UWeek number (Sunday start)51
%WWeek number (Monday start)52
%cLocale date & timeWed Dec 24 17:30:45 2025
%xLocale date12/24/25
%XLocale time17:30:45

Timezone

CodeMeaningExample
%zUTC offset+0530
%ZTimezone nameIST

Literal Percent

CodeMeaning
%%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.

Test your strftime format interactively

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

FunctionDirectionPurpose
strftimedatetime → stringDisplay or export dates
strptimestring → datetimeParse 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

  • %d gives 01
  • %-d (Linux only) gives 1

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top