Hit enter after type your search item

A little about Python strftime() function

The strftime() function belongs to datetime and time() modules in Python.

As the name suggests (“str” “f” “time”), Python strftime() function returns the string representation of the date. The strftime() takes an argument where you may specify the format of the date to be returned.

The last section shows the useful list of available directives that you may use for formatting dates.

Syntax of using strftime() function

The syntax of using the strftime() function in datetime module is:


By using time module

There is a difference between the syntax in two modules. The strftime() syntax as using time()module is:

time.strftime(format[, t])

The format argument is the same as in above case i.e. you may use date formatting directives for returning a date in a specified format.

The time.strftime function converts a tuple or struct_time to a string as specified by the format argument. The tuple or struct_time represents a time as returned by localtime() or gmtime() functions.

If you omit the t argument, the current local time is used by time.strftime() function.

The next section shows a few examples in different date format by using both modules. First, let me start with the datetime.strftime().

An example of datetime.strftime in DD/MM/YYYY format

The example below only displays the date in the DD/MM/YYYY format. For example, 20/12/2017. The current date is obtained by using the now() function of the datetime module as follows:

The output should be like:


The example of date with full day name

This example formats the date in full day name (%A), day number with zero padded decimal (%d), abbreviated month name (%b) and year without the century (%y) as follows:

Thursday, 06 Dec 18

The example of the current date and time in mm-dd-yyyy format

In this examples, the now() function is used to get the current date and time and strftime() will display in the following format:

The output should be like this:

Current Date and Time:  12-06-2018 20:07:09

List of useful directives with the output

The example below displays date by using various directives used in the strftime() function. Again, the current date is used and you can see directive description towards the left and output towards the right:

The output:

Python strftime

An example of time module’s strftime()

In this example, the localtime() function is used in the time’s strftime() function:

The output as I executed this code:

Dec 08 2018 12:04:25

Using gmtime() function example

You may also provide gmtime() function as the second argument as follows:

The result:

Dec 08 2018 07:06:01

Using various directives in time’s strftime() function

In this example, the mktime() function is used with the strftime() function with almost all available directives:

The output:

Python strftime time

You may learn more about the mktime() function that requires nine arguments, here.

This div height required for enabling the sticky sidebar