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. The syntax of the Python strftime function 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 Date and TimeThu 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 Version 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.

# Python strftime Example
from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time          = ', dt)
 
print('Local Version of Date and Time = ', dt.strftime('%c'))
print('Local Version of Date          = ', dt.strftime('%x'))
print('Local Version of Time          = ', dt.strftime('%X'))

strftime function to return date and time

Current Date and Time = 2021-05-02 16:00:01.379106
Local Version of Date and Time = Sun May 2 16:00:01 2021
Local Version of Date = 05/02/21
Local Version of Time = 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 Date and Time = ', dt)
 
print('Current Year          = ', dt.strftime('%Y'))
print('Month Name            = ', dt.strftime('%B'))
print('Day Number            = ', dt.strftime('%d')) 
Current Date and Time = 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('Current Date and Time    = ', 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      = ', 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 Python strftime function.

Current Date and Time = 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 = 17
Short Version of Weekday = Sun
Full Version of Weekday = Sunday
Weekday as a Number = 0

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

from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time = ', 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 Hours, Minutes, Seconds, Microseconds using Python strftime output

Current Date and Time = 2021-05-02 15:57:46.656409
Hours = 15
Hours = 03
Minutes = 57
Seconds = 46
Microseconds = 656409
AM or PM = PM

We are using all the available Python strftime directives in one example. It might helps you to see all of them in one place.

from datetime import datetime
 
dt = datetime.now()
 
print('Today\'s Date and Time             = ', dt)
 
print('Local Version of Date and Time     = ', dt.strftime('%c'))
print('Local Version of Date              = ', dt.strftime('%x'))
print('Local Version of Time              = ', dt.strftime('%X'))
 
print('Short Version of the Year          = ', dt.strftime('%y'))
print('Full Version of the Year           = ', dt.strftime('%Y'))
 
print('Short Version of Month Name        = ', dt.strftime('%b'))
print('Full Version of Month Name         = ', dt.strftime('%B'))
print('Month Number from 01 to 12         = ', dt.strftime('%m'))
 
print('Day Number of the Month from 01-31 = ', dt.strftime('%d'))
print('Day Number of Year from 001 to 366 = ', dt.strftime('%j'))
 
print('Week Number of Year from 00 to 53  = ', dt.strftime('%U'))
print('Week Number of Year from 00 to 53  = ', dt.strftime('%W'))
 
print('Short Version of Weekday           = ', dt.strftime('%a'))
print('Full Version of Weekday            = ', dt.strftime('%A'))
print('Weekday as a Number from 0 to 6    = ', dt.strftime('%w'))
 
print('Hours from 00 to 23 (24hr Format)  = ', dt.strftime('%H'))
print('Hours from 00 to 12 (12hr Format)  = ', dt.strftime('%I'))
print('Minutes from 00 to 59              = ', dt.strftime('%M'))
print('Seconds from 00 to 59              = ', dt.strftime('%S'))
print('Microseconds from 000000 to 999999 = ', dt.strftime('%f'))
print('AM or PM                           = ', dt.strftime('%p'))
print('% Charcter                         = ', dt.strftime('%%'))

Available strftime directives output

Today's Date and Time = 2021-05-02 15:55:13.539316
Local Version of Date and Time = Sun May 2 15:55:13 2021
Local Version of Date = 05/02/21
Local Version of Time = 15:55:13
Short Version of the Year = 21
Full Version of the Year = 2021
Short Version of Month Name = May
Full Version of Month Name = May
Month Number from 01 to 12 = 05
Day Number of the Month from 01-31 = 02
Day Number of Year from 001 to 366 = 122
Week Number of Year from 00 to 53 = 18
Week Number of Year from 00 to 53 = 17
Short Version of Weekday = Sun
Full Version of Weekday = Sunday
Weekday as a Number from 0 to 6 = 0
Hours from 00 to 23 (24hr Format) = 15
Hours from 00 to 12 (12hr Format) = 03
Minutes from 00 to 59 = 55
Seconds from 00 to 59 = 13
Microseconds from 000000 to 999999 = 539316
AM or PM = PM
% Charcter = %

strftime to format DateTime

So far, we are using one directive to return one part of the Date. However, Python strftime 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('Current Date and Time  = ', dt)
 
print('Current Date and Time  = ', dt.strftime('%Y/%m/%d %H:%M:%S %p'))

strftime to format date and time output

Current Date and Time = 2021-05-02 15:54:05.603666
Current Date and Time = 2021/05/02 15:54:05 PM

Few more strftime formatting options.

from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time    = ', dt)
 
print('Formatted Date and Time  = ', dt.strftime('%Y/%M/%d %H:%M:%S %p'))
print('Formatted Date and Time  = ', dt.strftime('%m/%d/%y %H:%M:%S'))
print('Formatted Date and Time  = ', dt.strftime('%m-%d-%Y %H:%M:%S'))
print('Formatted Date and Time  = ', dt.strftime('%d-%m-%Y %I:%M:%S %p'))
 
print('Formatted Date and Time  = ', dt.strftime('%Y, %B %d'))
print('Formatted Date and Time  = ', dt.strftime('%Y, %b %d'))
 
print('Formatted Date and Time  = ', dt.strftime('%B %d %y'))
print('Formatted Date and Time  = ', dt.strftime('%B %d, %Y %I-%M-%S %p'))
Python strftime 7

Python strftime in time Module

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

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