Python strftime() function
Last Updated :
12 Apr, 2025
Working with dates and times in Python often requires formatting them into readable strings. Python provides the strftime() function lets us convert a datetime object into a formatted string using special format codes. These format codes represent different parts of the date and time-like year, month, weekday, hour, etc. It’s especially useful when we need to display dates in a specific style or generate timestamps in logs and reports. For example, we can get the current date-time in a specific format like this:
Python
from datetime import datetime
now = datetime.now()
f = now.strftime("%Y-%m-%d %H:%M:%S")
print(f)
Output2025-04-09 19:44:50
Explanation:
- %Y gives the full year, %m gives the month, %d gives the day.
- %H:%M:%S returns the hour, minute, and second in 24-hour time format.
Syntax
datetime_obj.strftime(format)
Parameters:
- format: A string consisting of format codes to specify the output format.
Return Type: It returns the string representation of the date or time object.
Examples of strftime() function
Example 1: Basic Date and Time Formatting
In this example, we demonstrate how to extract different parts of the date and time using various format codes like weekday names, month, year, hour, and day of the year.
Python
from datetime import datetime
now = datetime.now()
print(now)
# Format: Abbreviated weekday, month, 2-digit year
f_1 = now.strftime("%a %m %y")
print(f_1)
# Format: Full weekday, full year
f_2 = now.strftime("%A %m %Y")
print(f_2)
# Format: 12-hour time with AM/PM and seconds
f_3 = now.strftime("%I %p %S")
print(f_3)
# Format: Day of the year
f_4 = now.strftime("%j")
print(f_4)
OutputOriginal datetime: 2025-04-09 19:54:17.579688
Wed 04 25
Wednesday 04 2025
07 PM 17
099
Explanation:
- %a and %A represent the short and full weekday names.
- %I %p %S shows 12-hour time with seconds and AM/PM.
- %j represents the day of the year (001 to 366).
Example 2: Formatting Date and Time with AM/PM
This example focuses on converting date and time into more readable formats using full month names and 12-hour format with AM/PM.
Python
from datetime import datetime
date = datetime.now()
# Format: Full month name, day, year
fd = date.strftime("%B %d, %Y")
print(fd)
# Format: Time with AM/PM
ft = date.strftime("%I:%M:%S %p")
print(ft)
OutputApril 09, 2025
08:01:38 PM
Explanation:
- %B provides the full month name.
- %I:%M:%S %p gives the time in 12-hour format with AM or PM.
Example 3: Combining Multiple Format Codes
This example combines various formatting codes to create custom, readable output strings—such as a sentence-like date or a common logging format.
Python
from datetime import datetime
now = datetime.now()
cf = now.strftime("Today is %A, %B %d, %Y")
print(cf)
# Common log format: DD/MM/YYYY HH:MM:SS
lf = now.strftime("%d/%m/%Y %H:%M:%S")
print(lf)
OutputToday is Wednesday, April 09, 2025
09/04/2025 20:02:49
Explanation:
- The first string uses text and format codes to build a human-friendly sentence.
- The second follows a typical log timestamp format used in many systems.
List of Format Codes
Here’s a reference table of the most commonly used strftime() format codes:
Directive | Meaning | Output Format |
---|
%a | Abbreviated weekday name. | Sun, Mon,.... |
%A | Full weekday name. | Sunday, Monday,..... |
%w | Weekday as a decimal number. | 0, 1,....., 6 |
%d | Day of the month as a zero added decimal. | 01, 02,...., 31 |
%-d | Day of the month as a decimal number. | 1, 2,...., 30 |
%b | Abbreviated month name. | Jan, Feb,...., Dec |
%B | Full month name. | January, February,.... |
%m | Month as a zero added decimal number. | 01, 02,...., 12 |
%-m | Month as a decimal number. | 1, 2,....., 12 |
%y | Year without century as a zero added decimal number. | 00, 01,..., 99 |
%-y | Year without century as a decimal number. | 0, 1,..., 99 |
%Y | Year with century as a decimal number. | 2013, 2019 etc. |
%H | Hour (24-hour clock) as a zero added decimal number. | 00, 01,....., 23 |
%-H | Hour (24-hour clock) as a decimal number. | 0, 1,...., 23 |
%I | Hour (12-hour clock) as a zero added decimal number. | 01, 02,..., 12 |
%-I | Hour (12-hour clock) as a decimal number. | 1, 2,...,12 |
%p | Locale’s AM or PM. | AM, PM |
%M | Minute as a zero added decimal number. | 00, 01,...., 59 |
%-M | Minute as a decimal number. | 0, 1,..., 59 |
%S | Second as a zero added decimal number. | 00, 01,..., 59 |
%-S | Second as a decimal number. | 0, 1,...., 59 |
%f | Microsecond as a decimal number, zero added on the left. | 000000 - 999999 |
%z | UTC offset in the form +HHMM or -HHMM. | |
%Z | Time zone name. | |
%j | Day of the year as a zero added decimal number. | 001, 002,....., 366 |
%-j | Day of the year as a decimal number. | 1, 2,...., 366 |
%U | Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. | 00, 01,...., 53 |
%W | Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. | 00, 01,....., 53 |