If you need to parse dates and times in Python, there is no better library than dateutil
. The parser
module can parse datetime
strings in many more formats than can be shown here, while the tz
module provides everything you need for looking up timezones. When combined, these modules make it quite easy to parse strings into timezone-aware datetime
objects.
You can install dateutil
using pip
or easy_install
, that is, sudo pip install dateutil==2.0
or sudo easy_install dateutil==2.0
. You need the 2.0 version for Python 3 compatibility. The complete documentation can be found at http://labix.org/python-dateutil.
Let's dive into a few parsing examples:
>>> from dateutil import parser >>> parser.parse('Thu Sep 25 10:36:28 2010') datetime.datetime(2010, 9, 25, 10, 36, 28) >>> parser.parse('Thursday, 25. September 2010 10:36AM') datetime.datetime(2010, 9, 25, 10, 36) >>> parser.parse('9/25/2010 10...