The Python strftime function is one of the datetime and time module functions. This 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.
Directives | Description | Example Output |
---|---|---|
%a | Short Version of Weekday | Wed |
%A | Full Version of Weekday | Wednesday |
%b | Short Version of Month Name | Jan |
%B | Full Version of Month Name | January |
%c | Local Version of Dt and tm | Thu May 23 10:35:32 2019 |
%d | Day Number of a Month 01 to 31 | 23 |
%f | Microseconds 000000 to 999999 | 965060 |
%H | Hours 00 to 23 | 15 |
%I | Hours 00 to 12 | 10 |
%j | Day Number of the Year 001 to 366 | 320 |
%m | Month as a Number 01 to 12 | 05 |
%M | Minutes from 00 to 59 | 35 |
%p | AM or PM | AM |
%S | Seconds from 00 to 59 | 32 |
%U | Week Number of the Year from 00 to 53, where First Day of the Week = Sunday | 20 |
%w | Weekday as a Number from 0 to 6, Where 0 = Sunday | 4 |
%W | Week Number of the Year from 00 to 53, where First Day of the Week = Monday | 20 |
%x | Local Version of Date | 05/23/19 |
%X | Local of Time | 10:35:32 |
%y | Short Version of Year | 19 |
%Y | Full Version of the Year | 2019 |
%z | UTC Offset | |
%Z | Timezone | |
%% | A % character | % |
Python strftime Examples
We use the 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 strftime to return the Year, Month name, and Day Number from the current date.
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.
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, and Day Number from Today’s 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 strftime to return the Hours, Minutes, Seconds, Microseconds, AM or PM from the current DateTime class.
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.

time Module
You can also import the time library to use this strftime. Here, we used it 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