Definition of Python strftime
In this article, we are discussing strftime() function in Python. This function is present in both datetime and time module where the working of the strftime() function is the same in both the modules but has a different syntax. In this article, we will see strftime() is a function that is used to convert the date and time format returned by localtime() or gmtime() into readable string in a specified format. The datetime module is an in-built module in python. This strftime() function takes format as an argument where there are different kinds of formats specified in Python to return the date and time in a readable string according to the format specified in the argument.
Working of strftime() function in Python
This section will see the strftime() function, which converts the time object to a readable string in a specified format. This function is available in both the datetime module and time module in Python. Let us see below syntax and examples of strftime() in both the modules.
Syntax in datetime module:
do.strftime(format)
parameter:
- format: this is used to specify the string format, which means it has a list of directives that can be used for specifying the format string.
Syntax in time module:
t.strftime(format [,t])
parameter:
- t: this is used to specify the time in seconds.
- format: this is used to specify the string format, which means it has a list of directives that can be used for specifying the format string.
Let us see a list of directives or format codes:
- To represent the name of the weekday in the short form, then we use “%a”.
- To represent the names of the weekday in the full form then we use “%A”.
- To represent the names of the month in short forms then we use “%b”.
- To represent the names of the month in the full form then we use “%B”.
- To represent the year in decimal number without century, then we use “%-y”.
- To represent the year in the decimal number with a century, then we use “%Y”.
- To represent time in 24 hours format as a decimal number, then we use “%-H”.
- To represent time in 12 hours format as a decimal number, then we use “%-I”.
- To represent time in seconds as a decimal number then we use “%-S”.
- To represent the name of the time zone then we use “%Z”.
There are many such directives for specifying the format codes to convert the date-time format into a readable string format.
Examples of strftime() function in Python
Now let us see examples below that use the above format directive codes to convert the given time and time format to the readable string. We will example using both the datetime module and also using the time module. We should note that the range in this timestamps is 0 to 60. But in rare cases, which means the range for timestamp was from 0 to 61 for some historical reasons.
Example #1
Let us see an example for demonstrating the strftime() function using the datetime module in Python.
Code:
from datetime import datetime as dt
print("Python program to demonstrate strftime() function using datetime module")
print("\n")
now = dt.now()
print("The current date and time without formatting is as follows:")
print(now)
print("\n")
s = now.strftime("%a %m %y")
print("This will display shortform of weekday, month, year without century:")
print(s)
print("\n")
s = now.strftime("%A %-m %Y")
print("This will display fullform of weekday name, month name without zero,year with cetury: ")
print(s)
print("\n")
s = now.strftime("%-I %p %S")
print("This will display the time in 12-h format, prime meridiean, seconds:")
print(s)
print("\n")
s = now.strftime("%-j")
print("This will display the day of the year in decimal number:")
print(s)
Output:
In the above program, we can see that we are using the datetime module, and then we are creating the datetime object as “dt”, then we can call the function strftime() function. But to print the current date and time without using strftime(), then we use the now() function then; later, we have converted this format into a readable string using various format codes from the given above list.
Example #2
Let us see an example demonstrating strftime() function using the time module.
Code:
from time import gmtime, strftime
print("Program to demonstrate strftime() function using time module")
print("\n")
s = strftime("%a, %d %b %Y %H:%M:%S + 1010", gmtime())
print("This will display the simple format of date and time using format codes with short forms")
print(s)
print("\n")
s = strftime("%A, %D %B %Y %H:%M:%S + 0000", gmtime())
print("This will display the simple format of date and time using format codes with full forms")
print(s)
print("\n")
# this will show you the preferd date time format
s = strftime("%c")
print("This will display prefered date and time format")
print(s)
print("\n")
s = strftime("%C")
print("This will dispay the century in decimal number")
print(s)
print("\n")
s = strftime("%r, %z, %Z")
print("This will dispay the name of the time zones with time")
print(s)
Output:
In the above program, we can see we have an imported time module, and we are using strftime() function and the format codes as listed in the above-given list.
Conclusion
In this article, we conclude to use strftime() function; we need to either use the time module or datetime module. In this article, we saw that strftime() function is mainly used for displaying the date and time from time-related objects to the readable string using specified format codes or directives. In this article, we have also listed a few directives or format codes and their meaning.
Recommended Articles
This is a guide to the Python strftime. Here we discuss the working of strftime() function in python along with multiple examples and its code implementation. You may also have a look at the following articles to learn more –