Python date strftime() Method with Example

Python date.strftime() Method: Here, we are going to learn about the strftime() method of date class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on April 30, 2020

Python date.strftime() Method

date.strftime() method is used to manipulate objects of date class of module datetime.

It 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:

    import datetime

Class:

    from datetime import date

Syntax:

    strftime(format)

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.

Given below is a list of all the format codes available:

DirectiveMeaningExample
%aAbbreviated weekday name.Sun, Mon, ...
%A Full weekday name.Sunday, Monday, ...
%wWeekday as a decimal number.0, 1, ..., 6
%dDay of the month as a zero-padded decimal.01, 02, ..., 31
%-dDay of the month as a decimal number.1, 2, ..., 30
%bAbbreviated month name.Jan, Feb, ..., Dec
%BFull month name.January, February, ...
%mMonth as a zero-padded decimal number.01, 02, ..., 12
%-mMonth as a decimal number.1, 2, ..., 12
%yYear without century as a zero-padded decimal number.00, 01, ..., 99
%-yYear without century as a decimal number.0,1, 2, … , 99
%YYear with century as a decimal number.2020, 2019 etc
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23
%-HHour (24-hour clock) as a decimal number.0, 1, 2, ..., 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12
%-IHour (12-hour clock) as a decimal number.1,2,.., 12
%pLocal AM or PM.AM, PM
%MMinute as a zero-padded decimal number.00, 01, ..., 59
%-MMinute as a decimal number.0, 1, ..., 59
%SSecond as a zero-padded decimal number.00, 01, ..., 59
%-SSecond as a decimal number.0, 1, ..., 59
%fMicrosecond as a decimal number,zero-padded on the left.000000 - 999999
%zUTC offset in the form +HHMM or -HHMM.
%ZTime zone name.
%jDay of the year as a zero-padded decimal number.001, 002, ..., 366
%-jDay of the year as a decimal number.1, 2, ..., 366
%cAppropriate local date and time representationWed Oct 27 01:04:15 2020
%x Appropriate local date representation.09/30/13
%XAppropriate local time representation.09:07:06
%UWeek number of the year, with Sunday as the first day of the week00, 01, ..., 53
%WWeek number of the year, with Monday as the first day of the week00, 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:

## 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




Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.