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 Code | Meaning | Example Output |
|---|---|---|
%Y | Year (4-digit) | 2025 |
%y | Year (2-digit) | 25 |
%m | Month (2-digit) | 02 |
%B | Full month name | February |
%b | Abbreviated month name | Feb |
%d | Day of the month (2-digit) | 03 |
Time-Related Format Codes
| Format Code | Meaning | Example Output |
|---|---|---|
%H | Hour (24-hour, 00-23) | 14 |
%I | Hour (12-hour, 01-12) | 02 |
%M | Minutes (00-59) | 30 |
%S | Seconds (00-59) | 45 |
%p | AM/PM Indicator | PM |
Weekday and Week Number Codes
| Format Code | Meaning | Example Output |
|---|---|---|
%A | Full weekday name | Monday |
%a | Abbreviated weekday | Mon |
%w | Weekday as a number (0=Sunday, 6=Saturday) | 1 |
%U | Week number of the year (starting from Sunday) | 05 |
%W | Week number of the year (starting from Monday) | 05 |
Day of the Year and Miscellaneous Codes
| Format Code | Meaning | Example Output |
|---|---|---|
%j | Day of the year (001-366) | 034 |
%c | Locale’s full date and time | Mon Feb 3 14:30:45 2025 |
%x | Locale’s date representation | 02/03/25 |
%X | Locale’s time representation | 14: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 adatetimeobject.strftime("%A, %B %d, %Y at %I:%M %p")formats thedatetimeobject into a human-readable form.
8. Conclusion
- The
strftime()function formatsdatetimeobjects 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.