NLP | Using dateutil to parse dates.
Last Updated : 18 Jun, 2019
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.
Installation : 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 Python3 1== # importing library from dateutil import parser print (parser.parse('Thu Sep 25 10:36:28 2010')) print (parser.parse('Thursday, 25. September 2010 10:36AM')) print (parser.parse('9 / 25 / 2010 10:36:28')) print (parser.parse('9 / 25 / 2010')) print (parser.parse('2010-09-25T10:36:28Z'))
Output : 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.
Python3 1== parser.parse('16 / 6/2019', dayfirst = True)
Output : 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.
Python3 1== print (parser.parse('11-6-19')) print (parser.parse('10-6-25', yearfirst = True))
Output : 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.
Similar Reads
How to Parse a Time String Containing Milliseconds in Python?
In many applications, precise timekeeping is crucial, and that often includes parsing time strings down to the millisecond. Python offers robust tools for handling date and time data, making this task straightforward. This article will guide us through the process of parsing time strings containing
3 min read
Introduction to Python Dateutil Package
The Dateutil is a Python package that enhances Python's built-in datetime module and makes it more flexible and user-friendly when dealing with dates and times. In this article, we will learn about the basics of the python-dateutil package, including its installation, key features, and practical exa
3 min read
PHP | date_parse_from_format() Function
The date_parse_from_format() is an inbuilt function in PHP which is used to get information about given date formatted according to the specified format. The date_parse_from_format() function accepts two parameters and returns associative array with detailed information about given date. Syntax: arr
3 min read
DateFormat parse(string , ParsePosition) Method in Java with Examples
DateFormat class of java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time. Note: DateFormat class extends Form
3 min read
Formatting Dates in Python
In different regions of the world, different types of date formats are used and for that reason usually, programming languages provide a number of date formats for the developed to deal with. In Python, it is dealt with by using a liberty called DateTime. It consists of classes and methods that can
5 min read
JSTL Formatting <fmt:parseDate> Tag
In JSTL, the formatting <fmt:parseDate> tag is used to parse the date strings into proper java.util.Date objects. This is useful if we need to represent a string in a formatted Date object. Using the tag the tasks of date parsing and formatting can be easily done in Java Server Pages applicati
3 min read
Python - Extract date in String
Given a string, the task is to write a Python program to extract date from it. Input : test_str = "gfg at 2021-01-04" Output : 2021-01-04 Explanation : Date format string found. Input : test_str = "2021-01-04 for gfg" Output : 2021-01-04 Explanation : Date format string found. Method #1 : Using re.s
4 min read
Python - Validate String date format
Given a date format and a string date, the task is to write a python program to check if the date is valid and matches the format. Examples: Input : test_str = '04-01-1997', format = "%d-%m-%Y" Output : True Explanation : Formats match with date. Input : test_str = '04-14-1997', format = "%d-%m-%Y"
3 min read
MonthDay parse(CharSequence, DateTimeFormatter) method in Java
The parse(CharSequence text, DateTimeFormatter formatter) method of the MonthDay class in Java is used to get an instance of MonthDay from a text string using a specific formatter. Syntax: public static MonthDay parse( CharSequence text, DateTimeFormatter formatter) Parameters: This method accepts t
2 min read
Perl - Extracting Date from a String using Regex
In Perl generally, we have to read CSV (Comma Separated Values) files to extract the required data. Sometimes there are dates in the file name like sample 2014-02-12T11:10:10.csv or there could be a column in a file that has a date in it. These dates can be of any pattern like YYYY-MM-DDThh:mm:ss or
5 min read