The time.strftime() function is used to convert a struct_time object to a string according to a specified format. 

Before we look at how the function works it is worth first understanding what struct_time objects are.

struct_time objects

struct_time is a class defined in the time module, meant for representing timestamps. A struct_time object  stores a timestamp broken down into 9 basic components, i.e year, month, day of month, hour, minute, second, day of week, day of year, and dst flag (daylight savings). We can then access each of these basic components of a timestamp using the various attributes defined by the struct_time object. 

time_struct attribute value stored
tm_year Calender year
tm_mon month
tm_mday day
tm_hour hour
tm_min minute
tm_sec second
tm_wday week day
tm_yday year day
isdst daylight savings

Some useful functions in the time module such as  localtime() and gmtime() converts a timestamp into a  struct_time objects.

import time

now = time.time()

#convert the timestamp 'now' in to struct_time object using localtime()
time_obj = time.localtime(now)
print(time_obj)

The work of the strftime function.

The strftime function converts a struct_time object into a formatted string.

The basic syntax for the function is as follows:

time.strftime(format, t = None)
format A required string defining the format of the outputted date/time string
t An optional argument representing the time value as a struct_time object.

The function returns a string representing the date/time represented by t according to the given format.

Using the strftime function to format a timestamp

import time

now = time.localtime()

time_obj = time.struct_time(now)

#format the timestamp with strftime()
formatted_time = time.strftime('%Y/%m/%d  %I:%M:%S %p', time_obj)

print(formatted_time)

As you have seen above, by applying correct formatting parameters, the strftime() function automatically knows how to insert the relevant  struct_time attribute values into the output string. This is useful  because it eliminates the need to perform the formatting manually.

another example

import time

time_obj = time.localtime()

formatted_date = time.strftime("%d %B %Y  %I:%M:%S %p", time_obj)
print(formatted_date)

Format strings and the parameters

In order to fully utilize the power of the strftime() function, you will need to understand how to form the format strings and what work each parameter in the format does. 

Each formatting parameter is represented by a percent sign (%) followed by a letter. Each letter represents a different part of the date and time that can be included in the format string, such as the day, month, hour, second, etc. Consider the following simplified example:

import time

format_string = "%d-%m-%Y"

#Without an argument the function uses the current timestamp
formatted_date = time.strftime(format_string)

print(formatted_date)

In the above example, the format string "%d-%m-%Y" can be broken as follows:

  • the '%d' is used to denote the day of the month
  • the '%m' is used to denote the month
  • and the '%Y' is used to denote the year.
  • The  hyphen(-) is used as the date separator. 

The following table summarizes the various format parameters

parameter description
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%H Hour (24-hour clock) as a decimal number [00,23]
%I Hour (12-hour clock) as a decimal number [01,12]
%j Day of the year as a decimal number [001,366]
%m Month as a decimal number [01,12]
%M Minute as a decimal number [00,59]
%p Locale’s equivalent of either AM or PM
%S Second as a decimal number [00,61]
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]
%w Weekday as a decimal number [0(Sunday),6]
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]
%x Date representation.
%X Time representation.
%y Year without century, [00 - 99]
%Y Year with century as a decimal number
%z UTC offset in the form +HHMM or -HHMM
%Z Timezone name.