Home » Python Tutorial » Python Strftime Method with Examples

Python Strftime Method with Examples

The python strftime() method returns the string which represents the date, time objects. The strftime() method is defined in the datatime library. To use this function, we need to import the library in the source code. 

Table of Contents

How python strftime works?

As mentioned above, the strftime() method is defined in the datetime library. We need to import it into the code where we want to use the function. To use the strftime method(), different formats are predefined. We need to pass the format as a parameter to the function. Using those formats, we can retrieve the particular value from the string returned by the strftime() method.

Example using the formats

from datetime import datetime
current_time = datetime.now()

a = current_time.strftime("%a") #prints short weekday name
print("Today's day is "+a)

a = current_time.strftime("%A") #prints full weekday name
print("Today's day is "+a)

b = current_time.strftime("%b") #prints short month name
print("Name of this month is "+b)

b = current_time.strftime("%B") #prints full month name
print("Name of this month is "+b)

c = current_time.strftime("%c") #Locale's appropriate date and time representation.
print("The accurate time and date is "+c)

h = current_time.strftime("%H") #Hour (24-hour clock) as a zero-padded decimal number.
print("Hour in 24-hour format: "+h)

i = current_time.strftime("%I") #Hour (12-hour clock) as a zero-padded decimal number.
print("Hour in 12-hour format: "+i)

j = current_time.strftime("%j") #Day of the year as a zero-padded decimal number.
print("Day of the year: "+j)

m = current_time.strftime("%m") #Month as a zero-padded decimal number.
print("Number of month: "+m)

m = current_time.strftime("%M") #Minute as a zero-padded decimal number.
print("Minute: "+m)

p = current_time.strftime("%p") #Locale's equivalent of either AM or PM.
print("meridian: "+p)

s = current_time.strftime("%S") #Second as a zero-padded decimal number.
print("Second: "+s)

u = current_time.strftime("%U") #Week number of the year
print("Week number of the year: "+u)

w = current_time.strftime("%W") #Week number of the year
print("Week number of the year: "+w)

x = current_time.strftime("%x") #Locale's appropriate date representation.
print("Date: "+x)

x = current_time.strftime("%X") #Locale's appropriate time representation.
print("Time: "+x)

y = current_time.strftime("%y") #Year without century as a zero-padded decimal number.
print("Year: "+y)

y = current_time.strftime("%Y") #Year with century as a decimal number.
print("Year: "+y)

Output

Today's day is Mon
Today's day is Monday
Name of this month is Mar
Name of this month is March
The accurate time and date is Mon Mar 29 18:56:09 2021
Hour in 24-hour format: 18
Hour in 12-hour format: 06
Day of the year: 088
Number of month: 03
Minute: 56
meridian: PM
Second: 09
Week number of the year: 13
Week number of the year: 13
Date: 03/29/21
Time: 18:56:09
Year: 21
Year: 2021
Did you know?
1. Fibonacci Series In Python
2. Reverse a String in Python
3. Bubble Sort in Python
4. Slicing in Python
5. Recursion in Python
6. Matrix In Python
7. Binary Search in Python
8. Polymorphism in Python
9. Python return
10. Python Strftime

Pin It on Pinterest