datetime.strftime() Explained
datetime.strftime() formats a date or time object as a string.
Use it when you want to control how a date or time looks, such as:
2025-04-2204/22/2025April 22, 202514:30:00
This method is part of Python’s datetime tools. It works with datetime, date, and time objects. If you are new to the module, see the Python datetime module overview.
Quick example
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)
Example output:
2025-04-22 14:30:45
Use strftime() when you need to turn a datetime object into a formatted string.
What datetime.strftime() does
strftime():
- Converts a date or datetime object into a string
- Lets you control the output format
- Is useful for logs, filenames, reports, and user-friendly dates
- Works on
datetime,date, andtimeobjects
In other words, it takes a Python date/time value and returns text in the format you choose.
Basic syntax
object.strftime(format_string)
objectis usually adatetime,date, ortimeobjectformat_stringcontains special codes such as%Y,%m, and%d- The result is always a string
Example:
from datetime import datetime
now = datetime.now()
result = now.strftime("%Y-%m-%d")
print(result)
print(type(result))
Output:
2025-04-22
<class 'str'>
Notice that strftime() returns a string, not another datetime object.
Simple example
Here is a complete example step by step:
from datetime import datetime
meeting = datetime(2025, 7, 14, 9, 30, 0)
formatted = meeting.strftime("%B %d, %Y at %H:%M")
print("Original datetime:", meeting)
print("Formatted string:", formatted)
Output:
Original datetime: 2025-07-14 09:30:00
Formatted string: July 14, 2025 at 09:30
Important points:
meetingis adatetimeobjectformattedis a string- The original
meetingvalue does not change
Common format codes
These are some of the most useful strftime() format codes:
%Y= 4-digit year%m= month as number%d= day of month%H= hour in 24-hour format%M= minute%S= second%I= hour in 12-hour format%p= AM or PM%A= full weekday name%B= full month name
Example:
from datetime import datetime
now = datetime(2025, 4, 22, 14, 5, 9)
print(now.strftime("%Y")) # year
print(now.strftime("%m")) # month
print(now.strftime("%d")) # day
print(now.strftime("%H")) # hour (24-hour)
print(now.strftime("%M")) # minute
print(now.strftime("%S")) # second
print(now.strftime("%A")) # weekday name
print(now.strftime("%B")) # month name
Output:
2025
04
22
14
05
09
Tuesday
April
Common formatting patterns
These patterns are common in real programs.
ISO-style date
from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d"))
Example output:
2025-04-22
Readable date
from datetime import datetime
now = datetime.now()
print(now.strftime("%B %d, %Y"))
Example output:
April 22, 2025
Time only
from datetime import datetime
now = datetime.now()
print(now.strftime("%H:%M:%S"))
Example output:
14:30:45
12-hour time
from datetime import datetime
now = datetime.now()
print(now.strftime("%I:%M %p"))
Example output:
02:30 PM
Date and time together
from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
Filename-safe timestamp
This is useful because it avoids characters like : that can cause problems in filenames on some systems.
from datetime import datetime
now = datetime.now()
filename_stamp = now.strftime("%Y-%m-%d_%H-%M-%S")
print(filename_stamp)
Example output:
2025-04-22_14-30-45
If you need the current date and time first, see datetime.now() explained.
datetime.strftime() vs datetime.strptime()
These two methods do opposite jobs:
strftime()turns adatetimeinto a stringstrptime()turns a string into adatetime
Example:
from datetime import datetime
dt = datetime(2025, 4, 22, 14, 30)
text = dt.strftime("%Y-%m-%d %H:%M")
print(text)
Output:
2025-04-22 14:30
If you need to go the other way and convert text into a date, see datetime.strptime() explained.
Common beginner mistakes
Calling strftime() on a plain string
This will fail because strings do not have a strftime() method.
value = "2025-04-22"
print(value.strftime("%Y-%m-%d"))
You would get an error like:
AttributeError: 'str' object has no attribute 'strftime'
If your value is already text, you do not format it with strftime(). You may need to parse it first with strptime(). If you are unsure what a string is, see what is a string in Python.
Mixing up %m and %M
This is one of the most common mistakes.
%m= month%M= minute
Example:
from datetime import datetime
now = datetime(2025, 4, 22, 14, 7)
print(now.strftime("%Y-%m-%d")) # correct month
print(now.strftime("%Y-%M-%d")) # wrong: %M means minute
Output:
2025-04-22
2025-07-22
Using %H with %p and expecting 12-hour output
%His 24-hour format%Iis 12-hour format%pgivesAMorPM
Wrong idea:
from datetime import datetime
now = datetime(2025, 4, 22, 14, 30)
print(now.strftime("%H:%M %p"))
Output:
14:30 PM
Better:
from datetime import datetime
now = datetime(2025, 4, 22, 14, 30)
print(now.strftime("%I:%M %p"))
Output:
02:30 PM
Forgetting that the result is a string
After formatting, the value is text.
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d")
print(type(formatted))
Output:
<class 'str'>
That means you cannot use datetime-specific methods on the result unless you convert it back.
Using unsupported or mistyped format codes
If your output looks wrong, check your format string carefully. A small typo can change the result.
Useful checks:
from datetime import datetime
now = datetime.now()
print(type(now))
print(now)
print(now.strftime("%Y-%m-%d"))
print(now.strftime("%H:%M:%S"))
help(datetime.strftime)
Common causes of problems include:
- Using a format code with the wrong meaning
- Trying to format a value that is already a string
- Confusing formatting dates with parsing dates
- Expecting
strftime()to change the originaldatetimeobject
When to use strftime()
Use strftime() when you want to:
- Display dates in a readable format
- Build timestamps for logs
- Create filenames with the current date and time
- Prepare date text for reports or messages
It is a formatting tool. It does not create the date. It only changes how the date is shown as text.
FAQ
What does strftime mean in Python?
It means “string format time.” It formats a date or datetime object as a string.
What is the difference between strftime() and strptime()?
strftime() formats a datetime as text. strptime() parses text into a datetime.
Does strftime() return a string?
Yes. The result of strftime() is always a string.
Can I use strftime() with date objects?
Yes. You can use it with datetime, date, and time objects.
Why is my month wrong when using strftime()?
You may have used %M, which means minutes. Use %m for month.