Python strftime

The Python strftime function is one of the datetime module and time module function. This Python strftime function helps you to format the given dates into the required string format and the syntax of it in the datetime module is

datetime_Object_Value.strftime(Directives)

The syntax of the Python strftime function in the time module is

time.strftime(Directives, time_object_value)

The List of available directives and their descriptions in Python strftime function.

DirectivesDescriptionExample Output
%aShort Version of WeekdayWed
%AFull Version of WeekdayWednesday
%bShort Version of Month NameJan
%BFull Version of Month NameJanuary
%cLocal Version of Dt and tmThu May 23 10:35:32 2019
%dDay Number of a Month 01 to 3123
%fMicroseconds 000000 to 999999965060
%HHours 00 to 2315
%IHours 00 to 1210
%jDay Number of the Year 001 to 366320
%mMonth as a Number 01 to 1205
%MMinutes from 00 to 5935
%pAM or PMAM
%SSeconds from 00 to 5932
%UWeek Number of the Year from 00 to 53, where First Day of the Week = Sunday 20
%wWeekday as a Number from 0 to 6, Where 0 = Sunday4
%WWeek Number of the Year from 00 to 53, where First Day of the Week = Monday20
%xLocal Version of Date05/23/19
%XLocal of Time10:35:32
%yShort Version of Year19
%YFull Version of the Year2019
%zUTC Offset
%ZTimezone
%%A % character%

Python strftime Examples

We use the Python strftime function to return the local version of Date, Time, and DateTime of the current date and time.

from datetime import datetime
 
dt = datetime.now()
 
print(dt)
 
print('Local Version = ', dt.strftime('%c'))
print('Local Version of Dt          = ', dt.strftime('%x'))
print('Local Version of Tm          = ', dt.strftime('%X'))
2021-05-02 16:00:01.379106
Local Version = Sun May 2 16:00:01 2021
Local Version of Dt = 05/02/21
Local Version of Tm = 16:00:01

Using the Python strftime to return the Year, Month name, and Day Number from the current date.

from datetime import datetime
 
dt = datetime.now()
 
print('Current = ', dt)
 
print('Current Year          = ', dt.strftime('%Y'))
print('Month Name            = ', dt.strftime('%B'))
print('Day Number            = ', dt.strftime('%d')) 
Current = 2021-05-02 16:01:05.071968
Current Year = 2021
Month Name = May
Day Number = 02

This Python strftime example returns Year, Month name, Month Number, Day Number, etc., from today’s date and time.

from datetime import datetime
 
dt = datetime.now()
 
print(dt)
print('Year in Short            = ', dt.strftime('%y'))
print('Year                     = ', dt.strftime('%Y'))
print('Month Name in Short      = ', dt.strftime('%b'))
print('Month Name               = ', dt.strftime('%B'))
print('Month Number             = ', dt.strftime('%m'))
print('Day Number of the Month  = ', dt.strftime('%d'))
print('Day Number of the Year   = ', dt.strftime('%j'))
print('Week Number of Year from 00 to 53  = ', dt.strftime('%U'))
print('Week Number of Year      = ', dt.strftime('%W'))
print('Short Version of Weekday = ', dt.strftime('%a'))
print('Full Version of Weekday  = ', dt.strftime('%A'))
print('Weekday as a Number      = ', dt.strftime('%w'))

Get Year, Month name, Month Number, Day Number from Todays date using this function.

2021-05-02 15:58:43.549339
Year in Short = 21
Year = 2021
Month Name in Short = May
Month Name = May
Month Number = 05
Day Number of the Month = 02
Day Number of the Year = 122
Week Number of Year from 00 to 53 = 18
Week Number of Year = 17
Short Version of Weekday = Sun
Full Version of Weekday = Sunday
Weekday as a Number = 0

Let me use this Python strftime to return the Hours, Minutes, Seconds, Microseconds, AM or PM from current DateTime class.

from datetime import datetime
 
dt = datetime.now()
 
print(dt)
print('Hours                 = ', dt.strftime('%H'))
print('Hours                 = ', dt.strftime('%I'))
print('Minutes               = ', dt.strftime('%M'))
print('Seconds               = ', dt.strftime('%S'))
print('Microseconds          = ', dt.strftime('%f'))
print('AM or PM              = ', dt.strftime('%p'))
print('% Charcter           = ', dt.strftime('%%'))

Print Hours, Minutes, Seconds, Microseconds using this method output

2021-05-02 15:57:46.656409
Hours = 15
Hours = 03
Minutes = 57
Seconds = 46
Microseconds = 656409
AM or PM = PM
% Charcter = %

Python strftime to format DateTime

So far, we are using one Python directive to return one part of the Date. However, it is very useful to format the date and time in the required format. It displays the current date and time in Year/month/day Hour:Minute: Second AM/PM.

import datetime
 
dt = datetime.datetime.now()
 
print(dt)
 
print(dt.strftime('%Y/%m/%d %H:%M:%S %p'))
2021-05-02 15:54:05.603666
2021/05/02 15:54:05 PM

Few more formatting options.

Python strftime 7

time Module

You can also import the time library to use this Python strftime. Here, we used this function inside the time library.

import time
 
dt = time.localtime(1234567)
 
print(time.strftime('%Y/%M/%d %H:%M:%S %p', dt))
print(time.strftime('%m/%d/%y %H:%M:%S', dt))
print(time.strftime('%d-%m-%Y %I:%M:%S %p', dt))
 
print(time.strftime('%I:%M:%S %p', dt))
1970/26/15 12:26:07 PM
01/15/70 12:26:07
15-01-1970 12:26:07 PM
12:26:07 PM