strftime() function in Python

The Python library strftime() is used to format datetime objects into neat readable string representation. This is part of the datetime module and it can convert date and time into different formats using format specifiers.

1. Understanding strftime()

The function name strftime stands for “string format time”. It takes a datetime object and returns a string representation based on the specified format.

2. Importing Required Module

Since strftime() is part of the datetime module, you need to import it before using it.

Basic Import

from datetime import datetime

This imports the datetime class from the datetime module, allowing us to use its methods, including strftime().

3. Syntax of strftime()

datetime.strftime(format)
  • format – A string containing format codes that define how the date and time should be displayed.

Example:

from datetime import datetime

# Get the current date and time
now = datetime.now()

# Format the datetime object
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")

print("Formatted Date and Time:", formatted_time)

Output:

Formatted Date and Time: 2025-02-03 14:30:45

Here, %Y-%m-%d %H:%M:%S formats:

  • %Y – Year (4 digits)
  • %m – Month (2 digits)
  • %d – Day of the month
  • %H – Hour (24-hour format)
  • %M – Minutes
  • %S – Seconds

4. Common Format Codes in strftime()

The strftime() function uses format codes (placeholders) to determine how the output string should look.

Date-Related Format Codes

Format CodeMeaningExample Output
%YYear (4-digit)2025
%yYear (2-digit)25
%mMonth (2-digit)02
%BFull month nameFebruary
%bAbbreviated month nameFeb
%dDay of the month (2-digit)03

Time-Related Format Codes

Format CodeMeaningExample Output
%HHour (24-hour, 00-23)14
%IHour (12-hour, 01-12)02
%MMinutes (00-59)30
%SSeconds (00-59)45
%pAM/PM IndicatorPM

Weekday and Week Number Codes

Format CodeMeaningExample Output
%AFull weekday nameMonday
%aAbbreviated weekdayMon
%wWeekday as a number (0=Sunday, 6=Saturday)1
%UWeek number of the year (starting from Sunday)05
%WWeek number of the year (starting from Monday)05

Day of the Year and Miscellaneous Codes

Format CodeMeaningExample Output
%jDay of the year (001-366)034
%cLocale’s full date and timeMon Feb 3 14:30:45 2025
%xLocale’s date representation02/03/25
%XLocale’s time representation14:30:45

5. Formatting Dates and Times in Different Styles

Let’s see how strftime() is used to generate different styles of formatted output.

Example: Formatting Date in Different Styles

from datetime import datetime

now = datetime.now()

# Different formats
print("ISO Date Format:", now.strftime("%Y-%m-%d"))
print("Full Date:", now.strftime("%A, %B %d, %Y"))
print("12-hour format:", now.strftime("%I:%M:%S %p"))
print("24-hour format:", now.strftime("%H:%M:%S"))
print("Day of the Year:", now.strftime("%j"))
print("Week Number:", now.strftime("%U"))

Output:

ISO Date Format: 2025-02-03
Full Date: Monday, February 03, 2025
12-hour format: 02:30:45 PM
24-hour format: 14:30:45
Day of the Year: 034
Week Number: 05

6. Formatting a Specific Date

Instead of using the current date and time, we can format any specific date.

Example: Formatting a Custom Date

from datetime import datetime

# Define a custom date
custom_date = datetime(2023, 7, 14, 9, 45, 30)

# Format it
formatted_date = custom_date.strftime("%A, %d %B %Y at %I:%M %p")

print("Formatted Custom Date:", formatted_date)

Output:

Formatted Custom Date: Friday, 14 July 2023 at 09:45 AM

7. Converting String to datetime and Formatting It

Sometimes, we get a date and time as a string. We can first convert it to a datetime object using strptime(), then format it with strftime().

Example: Converting and Formatting a Date String

from datetime import datetime

# Convert string to datetime object
date_string = "03-02-2025 14:30:45"
date_object = datetime.strptime(date_string, "%d-%m-%Y %H:%M:%S")

# Format datetime object to a readable format
formatted_date = date_object.strftime("%A, %B %d, %Y at %I:%M %p")

print("Converted Date:", formatted_date)

Output:

Converted Date: Monday, February 03, 2025 at 02:30 PM

Here:

  • strptime("%d-%m-%Y %H:%M:%S") parses the string "03-02-2025 14:30:45" into a datetime object.
  • strftime("%A, %B %d, %Y at %I:%M %p") formats the datetime object into a human-readable form.

8. Conclusion

  • The strftime() function formats datetime objects into strings using format codes.
  • Customizing date and time formats is its feature.
  • It can be used along with strptime() to convert and format date strings.
  • It supports almost all kinds of format specifiers.