Last Updated : 12 Jul, 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)
2025-04-09 19:44:50
Explanation:
datetime_obj.strftime(format)
Parameters:
Return Type: It returns the string representation of the date or time object.
Examples of strftime() function Example 1: Basic Date and Time FormattingIn 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)
Original datetime: 2025-04-09 19:54:17.579688 Wed 04 25 Wednesday 04 2025 07 PM 17 099
Explanation:
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)
April 09, 2025 08:01:38 PM
Explanation:
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)
Today is Wednesday, April 09, 2025 09/04/2025 20:02:49
Explanation:
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,....., 53RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4