The parser module can parse datetime strings in many more formats. There can be no better library than dateutil to parse dates and times in Python. To lookup the timezones, the tz module provides everything. When these modules are combined, they make it very easy to parse strings into timezone-aware datetime objects.
dateutil can be installed using pip or easy_install, that is, sudo pip install dateutil==2.0 or sudo easy_install dateutil==2.0. 2.0 version for Python 3 compatibility is required. The complete documentation can be found at http://labix.org/python-dateutil.
Code: Parsing Examples
datetime.datetime(2010, 9, 25, 10, 36, 28) datetime.datetime(2010, 9, 25, 10, 36) datetime.datetime(2010, 9, 25, 10, 36, 28) datetime.datetime(2010, 9, 25, 0, 0) datetime.datetime(2010, 9, 25, 10, 36, 28, tzinfo=tzutc())
All it takes is importing the parser module and calling the parse() function with a datetime string. The parser can return a sensible datetime object, but it cannot parse the string, it will raise a ValueError.
How it works :
- The parser instead of looking for recognizable tokens, guess what those tokens refer to. It doesn’t use regular expressions.
- The order of these tokens matters as it uses a date format that looks like Month/Day/Year (the default order), while others use a Day/Month/Year format.
- The parse() function takes an optional keyword argument, dayfirst, which defaults to False to deal with this problem.
- It can correctly parse dates in the latter format if it is set to True.
datetime.datetime(2016, 6, 16, 0, 0)
Another ordering issue can occur with two-digit years. but ’11-6-19′ is an ambiguous date format. Since dateutil defaults to the Month-Day-Year format, ’11-6-19′ is parsed to the year 2019. But if yearfirst = True is passed into parse(), it can be parsed to the year 2011.
datetime.datetime(2019, 11, 6, 0, 0) datetime.datetime(2011, 6, 19, 0, 0)
dateutil parser can also do fuzzy parsing and allows to ignore extraneous characters in a datetime string. parse() will raise a ValueError with the default value of False, when it encounters unknown tokens. A datetime object can usually be returned, if fuzzy = True.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course