The datetime.strptime class method can parse a date/time string into a datetime object.
Table of contents
- Parsing strings into
datetimeobjects ➡️ - The
strptimeformatting directives 📑 - Just parse my string please! ⚡
- What time is it? It's "string parse time"⏱️
- Converting a
datetimeinto a string ⬅️ strptimeparses andstrftimeformats 🔄- f-strings for
datetimeformatting ✨ strptimefordateandtimeobjects? 🤔- Parse with
str-p-time, format withstr-f-time📝
Parsing strings into datetime objects ➡️
Here's an example of the strptime class method in action:
>>> fromdatetimeimportdatetime>>> datetime.strptime("Jun 1 2005 1:33PM","%b %d %Y %I:%M%p")datetime.datetime(2005, 6, 1, 13, 33)Note that strptime is a class method: we're calling it on the datetime class (not an instance of the class).
The strptime formatting directives 📑
Those single letter % codes …