Home »
Python »
Python Reference »
Python date Class
Python date strftime() Method with Example
Python date.strftime() Method: In this tutorial, we will learn about the strftime() method of date class in Python with its usage, syntax, and examples.
By Hritika Rajput Last updated : April 22, 2023
Python date.strftime() Method
The date.strftime() method takes an instance of the class and returns a string representing the date, which is controlled by an explicit format string. Since this class has attributes related to dates only, format codes referring to hours, minutes, or seconds have 0 values.
Module
The following module is required to use strftime() method:
import datetime
Class
The following class is required to use strftime() method:
from datetime import date
Syntax
The following is the syntax of strftime() method:
strftime(format)
Parameter(s)
The following are the parameter(s):
- format - it is the string format code based on which string representation and formatting occurs. For example, %Y, %m, %d etc. are format codes. This method takes one or more format codes as an argument and returns a formatted string based on that.
Format Codes
Given below is a list of all the format codes available:
Directive | Meaning | Example |
%a | Abbreviated weekday name. | Sun, Mon, ... |
%A | Full weekday name. | Sunday, Monday, ... |
%w | Weekday as a decimal number. | 0, 1, ..., 6 |
%d | Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
%-d | Day of the month as a decimal number. | 1, 2, ..., 30 |
%b | Abbreviated month name. | Jan, Feb, ..., Dec |
%B | Full month name. | January, February, ... |
%m | Month as a zero-padded decimal number. | 01, 02, ..., 12 |
%-m | Month as a decimal number. | 1, 2, ..., 12 |
%y | Year without century as a zero-padded decimal number. | 00, 01, ..., 99 |
%-y | Year without century as a decimal number. | 0,1, 2, … , 99 |
%Y | Year with century as a decimal number. | 2020, 2019 etc |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
%-H | Hour (24-hour clock) as a decimal number. | 0, 1, 2, ..., 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
%-I | Hour (12-hour clock) as a decimal number. | 1,2,.., 12 |
%p | Local AM or PM. | AM, PM |
%M | Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
%-M | Minute as a decimal number. | 0, 1, ..., 59 |
%S | Second as a zero-padded decimal number. | 00, 01, ..., 59 |
%-S | Second as a decimal number. | 0, 1, ..., 59 |
%f | Microsecond as a decimal number,zero-padded on the left. | 000000 - 999999 |
%z | UTC offset in the form +HHMM or -HHMM. | |
%Z | Time zone name. | |
%j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
%-j | Day of the year as a decimal number. | 1, 2, ..., 366 |
%c | Appropriate local date and time representation | Wed Oct 27 01:04:15 2020 |
%x | Appropriate local date representation. | 09/30/13 |
%X | Appropriate local time representation. | 09:07:06 |
%U | Week number of the year, with Sunday as the first day of the week | 00, 01, ..., 53 |
%W | Week number of the year, with Monday as the first day of the week | 00, 01, ..., 53 |
Please note all the codes which have time and timezone information, will have 0 values as date class only contains date attributes.
Return Value
The return type of this method is a string converted according to the format code.
Example of date strftime() Method in Python
## Python program explaining the
## use of strftime() method in date class
from datetime import date
## Creating an instance
x = date.today()
c = x.strftime("%c")
print("Date representation using strftime:", c)
print()
##You can extract information using these methods
print("Abbreviated weekday name:", x.strftime("%a"))
print("Full weekday name:", x.strftime("%A"))
print("Weekday as a decimal number:", x.strftime("%w"))
print("Day of the month as a zero-padded decimal:", x.strftime("%d"))
print("Full month name:", x.strftime("%B"))
print("Month as a decimal number:", x.strftime("%-m"))
print("Year with century as a decimal number:", x.strftime("%Y"))
print("What day of the year is it?", x.strftime("%-j"))
print("What is the week number of this day?", x.strftime("%U"))
##or x.strftime("%W")
print()
## Different ways of representing the Date
## as a date string
print("Output 1:", x.strftime("%d %m %Y"))
print("Output 2:", x.strftime("%-d %-m %y, %H %M %S"))
## time values will be 0
print("Output 3:", x.strftime("%a %d %B %Y"))
print("Output 4:", x.strftime("%A, %Y %m %d, %H:%M:%S"))
print("Output 5:", x.strftime("%x"))
## You can create any combination of the date
## representation you want
Output
Date representation using strftime: Thu Apr 30 00:00:00 2020
Abbreviated weekday name: Thu
Full weekday name: Thursday
Weekday as a decimal number: 4
Day of the month as a zero-padded decimal: 30
Full month name: April
Month as a decimal number: 4
Year with century as a decimal number: 2020
What day of the year is it? 121
What is the week number of this day? 17
Output 1: 30 04 2020
Output 2: 30 4 20, 00 00 00
Output 3: Thu 30 April 2020
Output 4: Thursday, 2020 04 30, 00:00:00
Output 5: 04/30/20