Python strftime() Function Format Directives with Example


Python provides the strftime() function in order to print the current of a given date in different formats. The full name of the strftime() is string format time which will print the Python date and time structure in string format according to the given format.

strftime() Function Syntax

The strftime() function is provided by the time library or module. So in order to use strftime() function, the time library should be imported. strftime() function do not return any value.

strftime(FORMAT)
  • `FORMAT` will set the format of the date and time string. There are a lot of different format options which are explained below in the `Format Code/Directive List` part.

strftime() Function Format Code/Directives List

strftime() function has very large format code/directives which are used as the format parameter. Below all of them are listed and all these format code/directives are provided as a string inside double-quotes.

Directive Meaning Example
%a Abbreviated weekday name. Sun, Mon, …
%A Full weekday name. Sunday, Monday, …
%w Weekday as a decimal number. 0, 1, …, 6
%d Day of the month as a zero-padded decimal. 01, 02, …, 31
%-d Day of the month as a decimal number. 1, 2, …, 30
%b Abbreviated month name. Jan, Feb, …, Dec
%B Full month name. January, February, …
%m Month as a zero-padded decimal number. 01, 02, …, 12
%-m Month as a decimal number. 1, 2, …, 12
%y Year without century as a zero-padded decimal number. 00, 01, …, 99
%-y Year without century as a decimal number. 0, 1, …, 99
%Y Year with century as a decimal number. 2013, 2019 etc.
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%-H Hour (24-hour clock) as a decimal number. 0, 1, …, 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12
%-I Hour (12-hour clock) as a decimal number. 1, 2, … 12
%p Locale’s AM or PM. AM, PM
%M Minute as a zero-padded decimal number. 00, 01, …, 59
%-M Minute as a decimal number. 0, 1, …, 59
%S Second as a zero-padded decimal number. 00, 01, …, 59
%-S Second as a decimal number. 0, 1, …, 59
%f Microsecond as a decimal number, zero-padded on the left. 000000 – 999999
%z UTC offset in the form +HHMM or -HHMM.
%Z Time zone name.
%j Day of the year as a zero-padded decimal number. 001, 002, …, 366
%-j Day of the year as a decimal number. 1, 2, …, 366
%U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, …, 53
%W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, …, 53
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 09/30/13
%X Locale’s appropriate time representation. 07:06:05
%% A literal ‘%’ character. %
LEARN MORE  How To Use Python sleep() Function?
strftime() Function Format
strftime() Function Format

strftime() Function Format

strftime() Function Format

strftime() Function Format
strftime() Function Format
strftime() Function Format
strftime() Function Format

Print Current Date and Time

We will start with a simple example where we will print the date and time by using strftime() function. We will provide "%m/%d/%Y, %H:%M:%S" format specifier which provides month, day, year, hour, minute, second information.

d = time.strftime("%m/%d/%Y, %H:%M:%S")
print(d)

#Will output following line
#    12/23/2019, 20:34:04

Print Current Date in mm-dd-yyyy Format

We can also print the current date in mm-dd-yyyy format by using the "%m-%d-%Y" format specifier.

d = time.strftime("%m-%d-%Y")

print(d)
# Will output 12-23-2019

Print Year

The current year can be expressed with the %Yformat directive. If we want to print only the last two digits of the year we can use %y format directive.

d = time.strftime("%Y")
print(d)
# Output will be 2019

d = time.strftime("%y")
print(d)
#Output will be 19

Print Month

We can print the month number like 5,7,9 by using %m format directive but if we need the month name we can use %B for full month name and %b for abbreviated month name.

d = time.strftime("%m")
print(d)
#Output will be 12

d = time.strftime("%B")
print(d)
#Output will be December

d = time.strftime("%b")
print(d)
#Output will be Dec

Print Day

We can print the current day number by using the %d. We can also print the full weekday name with %A and abbreviated weekday name with %a.

d = time.strftime("%d")
print(d)
#Output will be 23

d = time.strftime("%A")
print(d)
#Output will be Monday


d = time.strftime("%a")
print(d)
#Output will be Mon

Print Hour

We can print the hour information by using the %H format specifier which will present hour in 24-hour clock format. %-H can be used present hour in 24-hour clock format as a decimal number. %I is used to present in 12-hour format.

d = time.strftime("%H")
print(d)
#The output will be 21

d = time.strftime("%-H")
print(d)
#The output will be 21

d = time.strftime("%I")
print(d)
#The output will be 09

Print Minute

We can print the minute information by using the %M for a zero-padded decimal number or %-M for decimal number.

d = time.strftime("%M")
print(d)
#The output will be 22

d = time.strftime("%-M")
print(d)
#The output will be 22

Print Second

We can print the second information by using the %S for zero-padded decimal number and %-S as a decimal number.

d = time.strftime("%S")
print(d)
#The output will be 47

d = time.strftime("%-S")
print(d)
#The output will be 53

Print Microsecond

Microsecond information can be printed by using a %f format specifier. This information will be decimal number and zero-padded on the left.

LEARN MORE  How To Use Python sleep() Function?

Print Week Day

We can also print a weekday number as a decimal with a %w format specifier. The name of the day can be printed with %A as full or %a as an abbreviated weekday name.

d = time.strftime("%w")
print(d)
#The output will be 1

d = time.strftime("%A")
print(d)
#The output will be Monday

d = time.strftime("%a")
print(d)
#The output will be Mon

Print Month As Name

We can also print the month name by using %B which will print the full month name. The abbreviated month name can be printed with the %b format directive. The month number can be printed with the %m format specifier.

d = time.strftime("%B")
print(d)
#The output will be December


d = time.strftime("%b")
print(d)
#The output will be Dec

d = time.strftime("%m")
print(d)
#The output will be 12

Print Day As Name

We can also print the day name in full format by using %A and abbreviated by using %a format directives.

d = time.strftime("%A")
print(d)
# The output will be Monday


d = time.strftime("%a")
print(d)
# The output will be Mon

Print The Century

We can print the year information with the century by using %Y.

Print AM or PM

We can print the AM or PM information by using %p information. This can be especially useful while printing the hour in a 12-hour format.

d = time.strftime("%p")
print(d)
#The output will be PM

Print Current TimeZone

We can print the time zone information about the system by providing the %Z option. Also, %z can be used to print UTC offset in the form of +HHMM or -HHMM.

d = time.strftime("%Z")
print(d)
#The output will be PST


d = time.strftime("%z")
print(d)
#The output will be -0800

Leave a Comment