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.
- Comparing dates in Python
- Formatting Dates in Python
- Read, Write and Parse JSON using Python
- Python | Execute and parse Linux commands
- Python | How to Parse Command-Line Options
- Python | Parse a website with regex and urllib
- Python | Sort list of dates given as strings
- Python program to find number of days between two given dates
- Python | Difference between two dates (in minutes) using datetime.timedelta() method
- Serializer Fields - Django REST Framework
- How to Get Masters in Data Science in 2020?
- Minimum steps to make sum and the product of all elements of array non-zero
- Print all subsequences of a string in Python
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.