5.1.8.1 Creating Datetime Objects from Formatted Strings

Python 2.5

5.1.8.1 Creating Datetime Objects from Formatted Strings

The datetime class does not directly support parsing formatted time strings. You can use time.strptime to do the parsing and create a datetime object from the tuple it returns:

>>> s = "2005-12-06T12:13:14"
>>> from datetime import datetime
>>> from time import strptime
>>> datetime(*strptime(s, "%Y-%m-%dT%H:%M:%S")[0:6])
datetime.datetime(2005, 12, 6, 12, 13, 14)
See About this document... for information on suggesting changes.