Home » Datetime.Strftime() Function In Python

Datetime.Strftime() Function In Python

Datetime.Strftime() Function In Python

Python datetime.strftime() method is a powerful tool for formatting date and time objects into strings. This function allows you to specify a format string that dictates how the date and time information should be represented in the output string.

In simple terms, the strftime() method stands for “string format time”. It takes a datetime object as input and returns a string representing that datetime according to the format specified.

Syntax

datetime.strftime(format)
Python
  • datetime: A datetime object representing a specific date and time.
  • format: A string specifying the format for the output.

Real-Life Example: Birthday Reminder

Let’s say you have a program that reminds you of your friends’ birthdays. You have the birthdate of each friend stored as a datetime object, and you want to display these dates in a more readable format.

from datetime import datetime

# Friend's birthdate
birthdate = datetime(1990, 5, 20)

# Format the birthdate
formatted_date = birthdate.strftime("%B %d, %Y")

# Output
print("Your friend's birthday is on:", formatted_date)
Python

In this example, "%B %d, %Y" is the format string. It specifies that the output should display the full month name (%B), followed by the day of the month (%d), and the year (%Y). When you run this code, it will print: “Your friend’s birthday is on: May 20, 1990”.

Common Format Codes in Python Strftime()

  • %Y: Year (4 digits)
  • %m: Month (01-12)
  • %d: Day of the month (01-31)
  • %H: Hour (00-23)
  • %M: Minute (00-59)
  • %S: Second (00-59)
  • %b: Abbreviated month name (e.g., Jan, Feb)
  • %A: Full weekday name (e.g., Monday, Tuesday)
  • %p: AM or PM

Certainly! Here are a few more examples demonstrating the usage of datetime.strftime() with different format strings:

Example 1: Current Date and Time


from datetime import datetime

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

# Format the current date and time
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")

# Output
print("Current date and time:", formatted_datetime)

Python

This code will print the current date and time in the format: “YYYY-MM-DD HH:MM:SS”.

Example 2: Displaying Weekday and Month Abbreviations


from datetime import datetime

# Some date
some_date = datetime(2023, 8, 15)

# Format the date
formatted_date = some_date.strftime("%A, %b %d, %Y")

# Output
print("Formatted date:", formatted_date)

Python

This code will output the date with the full weekday name, abbreviated month name, day of the month, and year, like “Monday, Aug 15, 2023”.

Example 3: Displaying Time in 12-Hour Format with AM/PM


from datetime import datetime

# Some time
some_time = datetime(2023, 8, 15, 14, 30, 0)

# Format the time
formatted_time = some_time.strftime("%I:%M %p")

# Output
print("Formatted time:", formatted_time)

Python

This code will output the time in 12-hour format with AM/PM indicator, like “02:30 PM”.

Example 4: Custom Date Representation


from datetime import datetime

# Some date
some_date = datetime(2023, 8, 15)

# Format the date
formatted_date = some_date.strftime("Year: %Y, Month: %m, Day: %d")

# Output
print("Custom formatted date:", formatted_date)

Python

This code will output the date with custom labels for year, month, and day, like “Year: 2023, Month: 08, Day: 15”.

These examples illustrate the flexibility of datetime.strftime() in formatting datetime objects into strings according to specific requirements. By adjusting the format string, you can customize the output to meet various needs in your Python applications.

Conclusion

The datetime.strftime() method is a powerful tool in Python for formatting datetime objects into strings. By providing a format string containing various format codes, you can customize the output to represent dates and times in a wide range of formats. Whether you need to display dates in a specific format, generate reports, or simply present time information in a user-friendly manner, strftime() offers the flexibility and versatility to meet your requirements. With its straightforward usage and extensive range of format codes, datetime.strftime() is an essential feature for working with date and time data in Python applications.

Frequently Asked Questions

Q1. Can I format datetime objects into strings using any format I want?

Ans: Yes, datetime.strftime() allows you to specify a wide range of format codes to customize the representation of datetime objects according to your needs.


Q2. What happens if I use an invalid format string with datetime.strftime()?

Ans: If you provide an invalid format string, datetime.strftime() will raise a ValueError exception.


Q3. Can I combine multiple format codes in a single format string?

Ans: Yes, you can combine multiple format codes to create complex datetime representations. For example, "%Y-%m-%d %H:%M:%S" represents a format containing year, month, day, hour, minute, and second.


Q4. Can I use datetime.strftime() with timezone-aware datetime objects?

Ans: Yes, datetime.strftime() works with both timezone-aware and naive datetime objects.