Python 3 – time strftime() Method
When working with Python 3, we often deal with time and date values. One of the useful methods in Python’s time module is strftime(). This method is used to format a specified date and time as per the given format string. It returns the formatted string representation of the date and time.
Here’s the syntax of the strftime() method:
strftime(format)
Parameters
The strftime() method takes one parameter:
format
– a string containing the date and time format codes.
Return Value
The strftime() method returns a string representing the date and time in the specified format.
Example 1: Format Current Date and Time
Let’s see an example where we use the strftime() method to format the current date and time.
import time
current_time = time.localtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)
print("Formatted Time:", formatted_time)
Output:
Formatted Time: 2021-11-16 00:29:35
In the above code, we first use the localtime() function to get the current date and time in the local timezone. We then use the strftime() method to format the current time as per the “%Y-%m-%d %H:%M:%S” format string. The resulting formatted time string is then printed on the console.
Example 2: Format Given Date
Sometimes, we may want to format a given date as per our desired format. In such cases, we can use the strptime() method to convert the string representation of the date into a time tuple and then use the strftime() method to format it in our desired format.
import time
date_string = "2021-11-16 00:29:35"
date_tuple = time.strptime(date_string, "%Y-%m-%d %H:%M:%S")
formatted_date = time.strftime("%A, %d %B %Y", date_tuple)
print("Formatted Date:", formatted_date)
Output:
Formatted Date: Tuesday, 16 November 2021
In the above code, we first convert the given date string into a time tuple using the strptime() method. We then use the strftime() method to format the date tuple in the format “%A, %d %B %Y”, which is the format for displaying the full weekday name, day of the month, full month name, and year. The resulting formatted date string is then printed on the console.
Format Codes
The strftime() method uses a set of format codes to determine the format of the resulting string. Here are some of the most commonly used format codes:
%Y
– 4-digit year%y
– 2-digit year%m
– month as a zero-padded decimal number (01-12)%B
– full month name%b
– abbreviated month name%d
– day of the month as a zero-padded decimal number (01-31)%A
– full weekday name%a
– abbreviated weekday name%H
– hour (00-23)%I
– hour in 12-hour format (01-12)%p
– AM/PM designation%M
– minute (00-59)%S
– second (00-59)
These format codes can be combined with other characters to create any desired output format. For example, to display the date and time in the format “MM/DD/YYYY HH:MM:SS PM”, we can use the format string “%m/%d/%Y %I:%M:%S %p”.
Conclusion
The strftime() method is a useful tool for formatting date and time values in Python 3. It allows us to convert a time tuple into a string representation with our desired format. By using format codes, we can create any desired format for displaying the date and time.