Strptime python is used to convert string to datetime object.

Python Strptime Format

strptime(date_string, format)


strptime("9/23/20", "%d/%m/%y")

Note - format "%d/%m/%y" represents the the corresponding "9/23/20" format. The output of the above command will be a Python datetime object.

The format is constructed using pre-defined codes. There are many codes to choose from. The most important ones are listed below.

Code Meaning
%a Weekday as Sun, Mon
%A Weekday as full name as Sunday, Monday
%w Weekday as decimal no as 0,1,2...
%d Day of month as 01,02
%b Months as Jan, Feb
%B Months as January, February
%m Months as 01,02
%y Year without century as 11,12,13
%Y Year with century 2011,2012
%H 24 Hours clock from 00 to 23
%I 12 Hours clock from 01 to 12
%p AM, PM
%M Minutes from 00 to 59
%S Seconds from 00 to 59
%f Microseconds 6 decimal numbers

Python Datetime Strptime

Example: Convert date string to Python datetime object.

import datetime
datetime.datetime.strptime("09/23/2030 8:28","%m/%d/%Y %H:%M")

Result of above command...

datetime.datetime(2030, 9, 23, 8, 28)

Also checkout Strftime Python to convert Python datetime object to string.