How to Format Dates in Python

How to Format Dates in Python

In Python, you can use the strftime() method of datetime objects to format dates as strings according to specified format codes.

Before diving into specific formatting examples using Python’s datetime module, it’s important to understand the format codes used in the strftime() method, which allow you to specify exactly how you want the date and time to be presented. Here are some of the most frequently used format codes that you will encounter:

  • %Y: Year with century (e.g., 2024)
  • %m: Month as a number (01-12)
  • %d: Day of the month (01-31)
  • %H: Hour (00-23)
  • %M: Minute (00-59)
  • %S: Second (00-59)
  • %A: Full weekday name (e.g., Wednesday)
  • %B: Full month name (e.g., July)
  • %I: Hour (01-12)
  • %p: Locale’s AM or PM

For a full list of format codes, please visit the attached link to the official Python documentation. 

Basic Date Formatting

Let’s start with the basics of formatting dates. Using the strftime() method from the datetime class, you can convert a datetime object into a formatted string according to your needs:

from datetime import datetime

# Create a sample date
sample_date = datetime(2024, 7, 24, 15, 30, 45)
print(sample_date)

# Basic formatting
print(sample_date.strftime("%Y-%m-%d"))  
print(sample_date.strftime("%d/%m/%Y"))  
print(sample_date.strftime("%B %d, %Y"))  

Output:

2024-07-24 15:30:45

2024-07-24
24/07/2024
July 24, 2024

These examples demonstrate how to format the date component in various ways, whether in the commonly used YYYY-MM-DD format, the European style DD/MM/YYYY, or a more verbose format that includes the full name of the month.

Including Time in the Format

Often, you’ll want to include the time along with the date. This can also be achieved with strftime() method in several intuitive ways:

# Including time in the format
print(sample_date.strftime("%Y-%m-%d %H:%M:%S"))  
print(sample_date.strftime("%I:%M %p, %d/%m/%Y")) 

Output:

2024-07-24 15:30:45
03:30 PM, 24/07/2024

The first format string includes a 24-hour clock representation, while the second uses a 12-hour clock with an AM/PM indicator. These formats are useful for logs, timestamps, and user interfaces that require time to be displayed alongside dates.

ISO 8601 Format

For international standards compliance, particularly in APIs and data storage, ISO 8601 format is essential:

# ISO 8601 format
print(sample_date.isoformat())  

Output:

2024-07-24T15:30:45

Using isoformat() provides a standard datetime string, which is crucial for ensuring compatibility across different systems and technologies.

Custom Formatting

For more complex needs, strftime() allows for custom formatting combinations, including day names and more:

# Custom formatting
custom_format = "%Y-%m-%d (%A) %H:%M"
print(sample_date.strftime(custom_format)) 

Output:

2024-07-24 (Wednesday) 15:30

This example includes the day of the week in the format, which might be particularly useful for event planning applications or scheduling interfaces where knowing the day of the week is important. 

By understanding and utilizing the versatile formatting options provided by Python’s datetime module, developers can tailor date and time representations to fit the needs of various applications, enhancing both functionality and user experience.

Leave a Reply

Your email address will not be published. Required fields are marked *