Introduction to Python dateutil
In this article, we will discuss an in-built Python module dateutil. In Python, dateutil is an extension module of the datetime module which is used for displaying the date and time. This dateutil module is used when we were in need to display timezone using the datetime module which does not support as it would be done in the time module. The dateutil has other different features that are provided in Python for computing relative deltas between two given date and datetime objects, another feature is this module is used for parsing the date and time in the string to date-time objects.
Working of dateutil Module in Python
In this section, we will see how to use the dateutil module along with its features and its examples. The dateutil module is a powerful extension of the datetime module with different features and this is also an in-built module in Python which is related to date and time tasks. The dateutil module can be installed from PyPI which means we can download this module from PyPI and then install it as.
pip install python-dateutil
The above statement can be run in Python 3 and above versions if PyPI is not available with a small change in the above statement and it is as follows:
pip3 install python-dateutil
Now let us see the dateutil module and its features along with examples. Firstly let us explore the computation of relative deltas using the dateutil module. The relative delta is used when we want to display the date and time of the later years or month or day. In general, relativedelta type can be applied to the current date and time to replace with some specified date-time objects or to specify some later interval of time.
Examples of Python dateutil
Given below are the examples mentioned:
Suppose how much time is left in years or months or days for the upcoming next festival or event we can print this using relativedelta.
from dateutil.relativedelta import * from dateutil.easter import * from dateutil.rrule import * from dateutil.parser import * from datetime import * print("program to demonstrate relative delta of dateutil module:") now = parse("Sun Jun 14 17:13:46 UTC 2020") today = now.date() print("Today is: %s" % today) year = rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=15,byweekday=FR).year print("Year with next Aug 15th on a Friday is: %s" % year) rdelta = relativedelta(easter(year), today) print("How far is the event day of that year: %s" % rdelta) print("And the event of that year is: %s" % (today+rdelta))
In the above program, we can see that we are importing relativedelta from dateutil module. I this program, we are trying to print some event which was conducted on June 14th, 2020 which was Sunday. So first we are displaying today’s date which was in the string format into date and time object using parse() function which is provided by the dateutil module itself. Then we are trying to print the same event that would be conducted in the next upcoming year with the same date and day and that would come in the year 2022. So it will calculate how many years, months, and days are left. Then it will print the date along with the day.
Now let us see another feature of the dateutil module which is used to parse the format of the string in date and time objects. Let us demonstrate the parse() function in the below example.
from dateutil import parser print("Program to demonstrate parse() function of dateutil:") print("The parsed date and time of the given string is as follows:") a = 'Sun Jun 14 10:36:28 2020' print (parser.parse(a)) b = 'Friday, 25. September 2008 10:36AM' print (parser.parse(b)) c = '3 / 25 / 2020 10:36:28' print (parser.parse(c)) d = '9 / 15 / 2015' print (parser.parse(d)) e = '2010-09-2T10:36:28Z' print (parser.parse(e))
In the above program, we can see that we are importing a parser module which has parse() function to convert the given date and time format in different string formats into particular date-time objects.
Now let us see another feature of dateutil module.
from dateutil.rrule import rrule, rruleset, MONTHLY rules = rruleset() rules.rrule(rrule(MONTHLY, bymonthday=1, count=3)) rules.rrule(rrule(MONTHLY, bymonthday=2, count=3)) for date in rules: print(date)
In the above program, we can see another feature using rule which is used to display the dates in a range that is mainly used for recurrence set generations. It is in general a module used for implementing the recurrence rule documentation including support for caching of results.
In this article, we conclude that the dateutil module in Python is an in-built module used for date and time-related tasks. In this article, we saw dateutil is an extension of the datetime module. This dateutil module is mainly used for parsing any string format to date-time object and is also used for displaying timezones which is not supported by datetime alone as a time module. In this article, we also saw the rule module uses the dateutil module to display the range or recurrence of rule documentation.
This is a guide to Python dateutil. Here we discuss the introduction and working of python dateutil along with examples respectively. You may also have a look at the following articles to learn more –