Python's strftime directives

Note: Examples are based on datetime.datetime(2013, 12, 25, 17, 15, 30)

Code Meaning Example
%a Weekday as locale’s abbreviated name. Wed
%A Weekday as locale’s full name. Wednesday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 3
%d Day of the month as a zero-padded decimal number. 25
%b Month as locale’s abbreviated name. Dec
%B Month as locale’s full name. December
%m Month as a zero-padded decimal number. 12
%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. 17
%I Hour (12-hour clock) as a zero-padded decimal number. 05
%p Locale’s equivalent of either AM or PM. PM
%M Minute as a zero-padded decimal number. 15
%S Second as a zero-padded decimal number. 30
%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. 359
%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. 51
%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. 51
%c Locale’s appropriate date and time representation. Wed Dec 25 17:15:30 2013
%x Locale’s appropriate date representation. 12/25/13
%X Locale’s appropriate time representation. 17:15:30
%% A literal '%' character. %

Source: Python’s strftime documentation.

Why? I need to use Python’s strftime rarely enough that I can’t remember it off the top of my head and never bookmark it but often enough to be annoyed with having to Google “python strftime” and then find the table above in the Python documentation. So I decided to put this reference page up.

Who? This was slapped together on a whim by Will McCutchen, who is just slouching on the shoulders of the awesome Python documentation team. The source code is on Github and pull requests are welcome!

When? Last updated 2014-04-21.

Fork me on GitHub