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.
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 Date and Time | 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 Version 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.
# 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'))
Using the 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'))
This Python 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'))
Let me use this Python 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'))
W are using all the available 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('%%'))
Python 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'))
Few more Python 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 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))