Python datetime strftime() Method with Example

Python datetime.strftime() Method: In this tutorial, we will learn about the strftime() method of datetime class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 22, 2023

Python datetime.strftime() Method

The datetime.strftime() method returns a string representing the date and time which is controlled by an explicit format string.

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 datetime

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:

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.N/A
%ZTime zone name.N/A
%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

Return Value

The return type of this method is a string converted according to the format code.

Example of datetime strftime() Method in Python

## Python program explaining the 
## use of strftime() method in datetime class

from datetime import datetime
import pytz

## Creating an instance
x = datetime.now()
timezone = pytz.timezone("Asia/Kolkata")
## Adding timezone information in datetime object
x = x.astimezone(timezone)
c = x.strftime("%c")
print("Date and time 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("What is the name of the timezone given", x.strftime("%Z"))
print("Is it AM or PM", x.strftime("%p"))
print("Time ahead of UTC time by:", x.strftime("%z"))
print()

## Different ways of representing the Date as a date string
print("Output 1: date:", x.strftime("%d/%m/%Y"))
print("Output 2: time:", x.strftime("%H:%M:%S:%f"))
print("Output 3: time with timezone:", x.strftime("%H:%M:%S:%f %z"))
print("Output 4: date with day: ", x.strftime("%a %d/%B/%Y"))
print("Output 5:", x.strftime("%A, %Y/%m/%d, %H:%M:%S:%f %Z"))
print("Output 6:", x.strftime("%x"))
print("Output 7:", x.strftime("%X"))
## You can create any combination of the date and time 
## representation you want 

Output

Date and time representation using strftime: Sun May  3 23:08:28 2020

Abbreviated weekday name: Sun
Full weekday name: Sunday
Weekday as a decimal number: 0
Day of the month as a zero-padded decimal: 03
Full month name: May
Month as a decimal number: 5
Year with century as a decimal number: 2020
What day of the year is it? 124
What is the week number of this day? 18
What is the name of the timezone given IST
Is it AM or PM PM
Time ahead of UTC time by: +0530

Output 1: date: 03/05/2020
Output 2: time: 23:08:28:024355
Output 3: time with timezone: 23:08:28:024355 +0530
Output 4: date with day:  Sun 03/May/2020
Output 5: Sunday, 2020/05/03, 23:08:28:024355 IST
Output 6: 05/03/20
Output 7: 23:08:28



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.