How to format dates in Python using strftime?

In this article, you will understand how date formatting in Python works and learn how to choose between different available character codes to correctly show the date to the user.
Python date formatting using strftime

Most programming language provides useful functions to format date and time.

The date is written as September 21, 2015 in some places and 21 Sep 2015 in others. This preference depends on a lot of factors like the user’s locale, the industry the user works in, space constraints etc.

Python provides a convenient method called strftime to handle the date formatting. Various codes are available that let you control how the date will look after conversion.

The best part is that these codes have been standardized over time and you can use them across programming languages. PHP for instance has an equivalent strftime function that works the same way.

Strftime syntax

Strftime accepts two parameters. The first compulsory parameter is the format string and the second optional parameter is the time to format. If the second parameter isn’t provided, it defaults to the current time.

time.strftime(format[, t])

Example

import time

t = (2015, 9, 21, 8, 58, 12, 1, 34, 0)
t = time.mktime(t)
print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))

Output

Sep 21 2015 08:58:12

Common Date Format Strings

Here are a few other common examples of character strings and their equivalent outputs

Format String Output
%m/%d/%Y 09/21/2015
%d/%m/%Y 21/09/2015
%Y-%m-%d 2015-09-21
%I:%M %p 08:58 AM
%b %d %Y Sep 21 2015
%d %B, %Y 21 September, 2015
%c Mon, Sep 21 2015 08:58:12 (Locale Appropriate DateTime)
%x 09/21/15 (Locale Appropriate Date)
%X 08:58:12  (Locale Appropriate Time)

Complete Character Code List

The table below shows all the codes that you can use to create the date format string.

Code Meaning Example
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a decimal number. (Platform specific) 30
%b Month as locale’s abbreviated name. Sep
%B Month as locale’s full name. September
%m Month as a zero-padded decimal number. 09
%-m Month as a decimal number. (Platform specific) 9
%y Year without century as a zero-padded decimal number. 13
%Y Year with century as a decimal number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 07
%-H Hour (24-hour clock) as a decimal number. (Platform specific) 7
%I Hour (12-hour clock) as a zero-padded decimal number. 07
%-I Hour (12-hour clock) as a decimal number. (Platform specific) 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded decimal number. 06
%-M Minute as a decimal number. (Platform specific) 6
%S Second as a zero-padded decimal number. 05
%-S Second as a decimal number. (Platform specific) 5
%f Microsecond as a decimal number, zero-padded on the left. 000000
%z UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).  
%Z Time zone name (empty string if the object is naive).  
%j Day of the year as a zero-padded decimal number. 273
%-j Day of the year as a decimal number. (Platform specific) 273
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 39
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 39
%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. %