Last modified: May 04, 2026 By Alexander Williams

Python Datetime Strftime Guide

Working with dates and times is a common task in Python. The strftime method helps you convert a datetime object into a readable string. This article explains everything you need to know about Python datetime strftime.

You will learn format codes, see practical examples, and avoid common mistakes. By the end, you can format dates for reports, logs, or user interfaces.

What is Python datetime strftime?

strftime stands for "string format time." It is a method of the datetime class. It takes a format string as input. It returns a formatted string representing the date and time.

The method is part of the datetime module. You must import the module first. Then you create a datetime object. Finally, you call strftime on it.

 
import datetime

# Get current date and time
now = datetime.datetime.now()
# Format it as a string
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)

2025-04-09 14:30:45

The output shows a clean date-time string. The format codes like %Y and %m control the output.

Common Format Codes in strftime

Format codes are the core of strftime. Each code represents a part of the date or time. Here are the most useful ones:

  • %Y – Year with century (e.g., 2025)
  • %m – Month as a zero-padded number (01-12)
  • %d – Day of the month (01-31)
  • %H – Hour in 24-hour format (00-23)
  • %M – Minute (00-59)
  • %S – Second (00-59)
  • %A – Full weekday name (e.g., Wednesday)
  • %B – Full month name (e.g., April)
  • %p – AM or PM
  • %I – Hour in 12-hour format (01-12)

You can combine these codes in any order. Use separators like dashes, slashes, or colons.

 
import datetime

now = datetime.datetime.now()

# Different formats
print(now.strftime("%A, %B %d, %Y"))      # Wednesday, April 09, 2025
print(now.strftime("%I:%M %p"))           # 02:30 PM
print(now.strftime("%d/%m/%Y"))           # 09/04/2025

Wednesday, April 09, 2025
02:30 PM
09/04/2025

How to Use strftime with datetime Objects

You can use strftime on any datetime object. This includes objects from datetime.now(), datetime.strptime(), or manual creation.

First, create a datetime object. Then apply strftime with your desired format.

 
import datetime

# Create a specific date
my_date = datetime.datetime(2025, 12, 25, 10, 30, 0)
# Format it
formatted = my_date.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)  # Output: 2025-12-25 10:30:00

2025-12-25 10:30:00

You can also format dates after parsing them. For example, use strptime to parse a string into a datetime object. Then format it with strftime. This is useful when you receive dates as strings from users or files. Learn more in our Python datetime strptime guide.

Formatting Dates for Different Locales

Python's strftime respects the system's locale. This means weekday and month names appear in the local language. You can change the locale with the locale module.

This is helpful for international applications. For example, show "April" in English or "Avril" in French.

 
import datetime
import locale

# Set locale to French
locale.setlocale(locale.LC_TIME, 'fr_FR.UTF-8')

now = datetime.datetime.now()
print(now.strftime("%A, %B %d, %Y"))

mercredi, avril 09, 2025

Note: Locale names vary by operating system. On Windows, use 'fr_FR' or 'french'. Test your locale before deployment.

Common Use Cases for strftime

strftime is used in many real-world scenarios. Here are three common ones:

  1. Logging timestamps – Format dates for log files with %Y-%m-%d %H:%M:%S.
  2. User interfaces – Show friendly dates like "April 9, 2025" to users.
  3. File naming – Create unique filenames with date parts, e.g., report_20250409.txt.
 
import datetime

now = datetime.datetime.now()

# Log format
log_time = now.strftime("[%Y-%m-%d %H:%M:%S]")
print(f"{log_time} User logged in")

# File name
file_name = f"report_{now.strftime('%Y%m%d')}.txt"
print(f"Saving to {file_name}")

[2025-04-09 14:30:45] User logged in
Saving to report_20250409.txt

Common Mistakes and How to Avoid Them

Beginners often make errors with strftime. Here are the top mistakes:

  • Wrong format codes – Using %y (two-digit year) instead of %Y (four-digit year). Double-check your codes.
  • Missing import – Forgetting to import datetime. Always import the module first.
  • Calling strftime on a date object – The date class also has strftime, but it lacks time components. Use datetime for full formatting.

Always test your format string with a sample datetime object. This catches errors early.

 
import datetime

# Mistake: using %y instead of %Y
now = datetime.datetime.now()
print(now.strftime("%y-%m-%d"))  # Output: 25-04-09 (two-digit year)

# Correct: using %Y
print(now.strftime("%Y-%m-%d"))  # Output: 2025-04-09 (four-digit year)

25-04-09
2025-04-09

Advanced Formatting with strftime

You can create complex formats for specific needs. For example, ISO 8601 format is common in APIs. Use %Y-%m-%dT%H:%M:%S for that.

You can also include literal text like "at" or "on". Just escape special characters if needed.

 
import datetime

now = datetime.datetime.now()

# ISO 8601 format
iso_format = now.strftime("%Y-%m-%dT%H:%M:%S")
print(f"ISO: {iso_format}")

# Custom with literal text
custom = now.strftime("Today is %A, %B %d, %Y at %I:%M %p")
print(custom)

ISO: 2025-04-09T14:30:45
Today is Wednesday, April 09, 2025 at 02:30 PM

For a complete list of format codes, check our Python datetime format guide. It covers every code with examples.

strftime vs strptime: What's the Difference?

Many beginners confuse strftime and strptime. The difference is simple:

  • strftime – Converts datetime to string. (f = format)
  • strptime – Converts string to datetime. (p = parse)

You use strftime when you have a datetime object and need a string. You use strptime when you have a string and need a datetime object. See our Python datetime to string guide for more conversion examples.

Conclusion

Python datetime strftime is a powerful tool for formatting dates and times. It turns complex datetime objects into human-readable strings. You now know the key format codes, common use cases, and how to avoid mistakes.

Practice with your own dates. Try different format combinations. Soon you will format dates with confidence. For a deeper dive, read our Master Python datetime guide.

Remember: strftime is your friend for output. Use it for logs, reports, and user interfaces. Happy coding!