Cheat Sheet

Python strftime function Formatting Cheat Sheet

A quick and easy reference to various formatting options using the strftime function in Python
Captain Salem 2 min read
Python strftime function Formatting Cheat Sheet
Format Syntax Example Result Representation
%a Fri Weekday as locale’s abbreviated name.
%A Friday Weekday as a locale’s full name.
%w 5 Weekday as a decimal value, with 0 being Sunday and 6 being Saturday.
%d 07 Day of the month as a zero-padded decimal number.
%-d 7 Day of the month as a decimal number
%b Jan Month as locale’s abbreviated name.
%B January Month as locale’s full name.
%m 01 Month as a zero-padded decimal number.
%-m 1 Month as a decimal number. (Platform specific)
%y 22 Year without century as a zero-padded decimal number.
%Y 2022 Year with century as a decimal number.
%H 08 Hour (24-hour clock) as a zero-padded decimal number.
%-H 8 Hour (24-hour clock) as a decimal number. (Platform specific)
%I 07 Hour (12-hour clock) as a zero-padded decimal number.
%-I 7 Hour (12-hour clock) as a decimal number. (Platform specific)
%p AM Locale’s equivalent of either AM or PM.
%M 01 Minute as a zero-padded decimal number.
%-M 1 Minute as a decimal number. (Platform specific)
%S 01 Second as a zero-padded decimal number.
%-S 1 Second as a decimal number. (Platform specific)
%f 000000 Microsecond as a decimal number, zero-padded on the left.
%z +0000 UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).
%Z UTC Time zone name (empty string if the object is naive).
%j 251 Day of the year as a zero-padded decimal number.
%-j 251 Day of the year as a decimal number. (Platform specific)
%U 36 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 in week 0.
%W 35 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.
%c Friday January 7 08:07:01 2022 Locale’s appropriate date and time representation.
%x 01/07/22 Locale’s appropriate date representation.
%X 08:07:01 Locale’s appropriate time representation.
%% % A literal '%' character.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.