Open In App

Python strftime() function

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
2025-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)

Output
Original 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)

Output
April 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)

Output
Today 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:

DirectiveMeaningOutput Format
%aAbbreviated weekday name.Sun, Mon,….
%AFull weekday name.Sunday, Monday,…..
%wWeekday as a decimal number.0, 1,….., 6
%dDay of the month as a zero added decimal.01, 02,…., 31
%-dDay of the month as a decimal number.1, 2,…., 30
%bAbbreviated month name.Jan, Feb,…., Dec
%BFull month name.January, February,….
%mMonth as a zero added decimal number.01, 02,…., 12
%-mMonth as a decimal number.1, 2,….., 12
%yYear without century as a zero added decimal number.00, 01,…, 99
%-yYear without century as a decimal number.0, 1,…, 99
%YYear with century as a decimal number.2013, 2019 etc.
%HHour (24-hour clock) as a zero added decimal number.00, 01,….., 23
%-HHour (24-hour clock) as a decimal number.0, 1,…., 23
%IHour (12-hour clock) as a zero added decimal number.01, 02,…, 12
%-IHour (12-hour clock) as a decimal number.1, 2,…,12
%pLocale’s AM or PM.AM, PM
%MMinute as a zero added decimal number.00, 01,…., 59
%-MMinute as a decimal number.0, 1,…, 59
%SSecond as a zero added decimal number.00, 01,…, 59
%-SSecond as a decimal number.0, 1,…., 59
%fMicrosecond as a decimal number, zero added on the left.000000 – 999999
%zUTC offset in the form +HHMM or -HHMM. 
%ZTime zone name. 
%jDay of the year as a zero added decimal number.001, 002,….., 366
%-jDay of the year as a decimal number.1, 2,…., 366
%UWeek 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
%WWeek 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


Next Article
Article Tags :
Practice Tags :

Similar Reads