Python date formatting and parsing

Basics

Details can be found in the official documentation

datetime.strftime(format)
date to string conversion [string format time]
strptime(string, format)
string to date conversion [string parse time]
Directive Description Example
%c Date and time (l) Tue Aug 16 21:30:00 1988
%x Date (l) 08/16/1988
%X Time (l) 21:30:00
%% Literal '%' %

(l): Locale dependent

Date

Directive Description Example
%a Weekday (l) Sun, Mon, ..., Sat
%A Weekday (l) Sunday, Monday, ..., Saturday
%w Weekday (Sunday = 0) 0, 1, ..., 6
%b Month (l) Jan, Feb, ..., Dec
%B Month (l) January, February, ..., December
%m Month 01, 02, ..., 12
%y Year 00, 01, ..., 99
%Y Year 1970, 1988, 2001, 2013
%d Day of the month 01, 02, ..., 31
%j Day of the year 001, 002, ..., 366
%U Week of the year (Starts Sunday) 00, 01, ..., 53
%W Week of the year (Starts Monday) 00, 01, ..., 53

(l): Locale dependent

Time

Directive Description Example
%H Hour 00, 01, ..., 23
%I Hour 01, 02, ..., 12
%p AM/PM (l) AM, PM
%M Minute 00, 01, ..., 59
%S Second 00, 01, ..., 59
%f Microsecond 000000, 000001, ..., 999999
%Z UTC offset (empty), UTC, EST, CST
%z Time zone (empty), +0000, -0400, +1030

(l): Locale dependent