Tutorial

strftime() in Python

3 min read

The method strftime() in Python is a function used to format date and time objects into a string representation. The name “strftime” stands for “string format time,” indicating its purpose of converting time-related data into a specified format. This method is part of the datetime module in Python’s standard library and is commonly used when working with dates and times.

Working of strftime() in Python

The strftime() method takes a format string as its parameter, which defines how the date and time components should be represented in the resulting string. The format string consists of various format codes, each representing a specific part of the date and time. These codes are prefixed with a percentage sign (“%“) to indicate that they should be interpreted as format codes.

Here’s a step-by-step explanation of how strftime() works:

  • Import the datetime module: Before using strftime(), we need to import the datetime module from Python’s standard library. This module provides classes and functions for working with dates and times.
from datetime import datetime
  • Create a datetime object: Next, we need to create a datetime object that represents the date and time you want to format. We can obtain the current date and time using the datetime.now() function or create a datetime object from specific values using the datetime() constructor.
now = datetime.now()  # Get the current date and time
  • Specify the format codes: The key part of using strftime() is defining the format codes that determine how the date and time components will be represented in the resulting string. The format codes are placeholders that are replaced with the corresponding values from the datetime object. Here are some commonly used format codes:
    1. %Y: Four-digit year (e.g., 2023)
    2. %m: Zero-padded month (e.g., 06 for June)
    3. %d: Zero-padded day of the month (e.g., 28)
    4. %H: Zero-padded hour in 24-hour format (e.g., 15 for 3 PM)
    5. %M: Zero-padded minute (e.g., 30)
    6. %S: Zero-padded second (e.g., 45)
  • Call strftime() with the format string: Once you have defined the format codes, you can pass them as a format string to the strftime() method, which is called on the datetime object. The strftime() method then processes the format string, replaces the format codes with the appropriate values, and returns the resulting formatted string.
formatted_date = now.strftime("%Y-%m-%d")  # Format date
formatted_time = now.strftime("%H:%M:%S")  # Format time
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")  # Format date and time

In the above example, we create three formatted strings: formatted_date, formatted_time, and formatted_datetime, representing the date, time, and combined date and time, respectively.

  • Output or use the formatted string: Finally, you can output the formatted string using print() or use it in your program as needed.
print("Formatted Date:", formatted_date)
print("Formatted Time:", formatted_time)
print("Formatted DateTime:", formatted_datetime)

By utilizing different combinations of format codes, we can control the precise representation of date and time in the resulting string. The strftime() method offers great flexibility and allows us to generate a wide range of formatted strings to suit our specific requirements.

Example to Demonstrate the Usage of Python strftime()

from datetime import datetime

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

# Format the date and time using strftime()
formatted_date = now.strftime("%Y-%m-%d")
formatted_time = now.strftime("%H:%M:%S")
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")

# Print the formatted values
print("Formatted Date:", formatted_date)
print("Formatted Time:", formatted_time)
print("Formatted DateTime:", formatted_datetime)

In this example, we import the datetime module and use the now() function to retrieve the current date and time. Then, we use strftime() to format the date and time components into strings based on the provided format codes.

The format codes used in the Python example have the following meanings:

  • %Y: Four-digit year (e.g., 2023)
  • %m: Zero-padded month (e.g., 06 for June)
  • %d: Zero-padded day of the month (e.g., 28)
  • %H: Zero-padded hour in 24-hour format (e.g., 15 for 3 PM)
  • %M: Zero-padded minute (e.g., 30)
  • %S: Zero-padded second (e.g., 45)

The output of the example would be something like:

Formatted Date: 2023-06-28
Formatted Time: 15:30:45
Formatted DateTime: 2023-06-28 15:30:45