Python strftime()



The strftime() method is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns their string representation.


Example: datetime to string using strftime()

In the following program, we will convert a datetime object containing the current date and time to different string formats.

from datetime import datetime

now = datetime.now()

year = now.strftime("%Y")
print("year:", year)

month = now.strftime("%m")
print("month:", month)

day = now.strftime("%d")
print("day:", day)

time = now.strftime("%H:%M:%S")
print("time:", time)

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

After executing the above code, the output will be as follows:

year: 2018
month: 10
day: 28
time: 06:15:26
date and time: 10/28/2018, 06:15:26

Above, year, day, time and date_time are strings, whereas now is a datetime object.


How strftime() works?

The strftime() method takes one or more format codes (%Y, %m, %d, etc) as an argument and returns a formatted string based on it.

Let us explain every step of the above example.

  • We imported the datetime class from the datetime module. The imported datetime class has access to the strftime() method.


    import datetime module in Python
  • We stored in the now variable the datetime object that contains the current date and time.


    datetime object containing current date and time
  • We used the strftime() method to create formatted strings.


    Python strftime() example
  • The strftime() method can accept more than one format codes.


    Python strftime() multi arguments example

Example: Creating string from a timestamp

from datetime import datetime

timestamp = 1494654246
date_time = datetime.fromtimestamp(timestamp)

print("Date time object:", date_time)

a = date_time.strftime("%m/%d/%Y, %H:%M:%S")
print("First pattern:", a)

a = date_time.strftime("%d %b, %Y")
print("Second pattern:", a)

a = date_time.strftime("%d %B, %Y")
print("Third pattern:", a)

a = date_time.strftime("%I%p")
print("Fourth pattern:", a)

After executing the above code, the output will be as follows:

Date time object: 2017-05-13 05:44:06
First pattern: 05/13/2017, 05:44:06
Second pattern: 13 May, 2017
Third pattern: 13 May, 2017
Fourth pattern: 05AM

Format Code List

The following table shows all the format codes that we can pass to the strftime() method.

Directive Meaning Example
%a Abbreviated weekday name. Mon, Tue, ...
%A Full weekday name. Monday, Tuesday, ...
%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, ..., 31
%b Abbreviated month name. Jan, Feb, ..., Dec
%B Full month name. January, February, ..., December
%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. 2015, 2017 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 from +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 Jan 26 08:15:20 2017
%x Locale's appropriate date representation. 05/28/17
%X Locale's appropriate time representation. 08:15:20
%% A literal '%' character. %

Example: Locale's appropriate date and time

from datetime import datetime

timestamp = 1494654246
date_time = datetime.fromtimestamp(timestamp)

print("Date time object:", date_time)

a = date_time.strftime("%c")
print("First pattern:", a)

a = date_time.strftime("%x")
print("Second pattern:", a)

a = date_time.strftime("%X")
print("Third pattern:", a)

After executing the above code, the output will be as follows:

Date time object: 2017-05-13 05:44:06
First pattern: Sat May 13 05:44:06 2017
Second pattern: 05/13/17
Third pattern: 05:44:06

The above format codes %c, %x and %X are used for locale's appropriate date and time representation.


We also recommend to check Python strptime(). The strptime() method is the opposite of the strftime(), it creates a datetime object from a string.



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.