In This Article
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 usingstrftime()
, we need to import thedatetime
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 adatetime
object that represents the date and time you want to format. We can obtain the current date and time using thedatetime.now()
function or create adatetime
object from specific values using thedatetime()
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 thedatetime
object. Here are some commonly used format codes:
%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)
- Call
strftime()
with the format string: Once you have defined the format codes, you can pass them as a format string to thestrftime()
method, which is called on thedatetime
object. Thestrftime()
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