Dateutil module in PythonAn Introduction to the Python dateutil moduleWhenever we start working with scripts that need real-time data, we have to use the dateutil module in order to retrieve or schedule data at a specific time or input data with the timestamp of retrieval. It could be a daunting activity to work with a lot of scripts and try to manipulate formats of date and time with the help of the default datetime module, considering the number of changes to the data retrieved we have to make. Providentially, the developers created the dateutil module to deliver the functionality to make life easier. In the following tutorial, we will understand the Python dateutil module in depth. Python modules are files that consist of certain definitions and statements. We can use these modules to break down a large program into small segments, and they are imported into programs to avail their functions. The dateutil module is among these predefined modules in the Python programming language utilized when we have to perform work associated with a certain date and time. So, let's get begun. Understanding the Python dateutil moduleThere is a core module available in the Python programming language known as the datetime module. We use this module for the manipulation of dates and times from simpler to complex methods. Even though this module is adequate for a specific number of instances, the Python dateutil module offers particular powerful extensions to the datetime module. Chief Characteristics of the Python dateutil moduleThe chief characteristics of the dateutil module in Python are as follows:
How to install the dateutil module in Python?Before we start working with the dateutil module, the primary objective is to install the module on the system. So, let's get to the installation procedure. We will be using the pip installer in order to install the required module by typing the following command in a terminal: Syntax: Verifying the InstallationIn order to check whether the module has been installed in the system properly or not, we can try importing the module and execute the program. Once the installation is complete, create a new Python file and type the following syntax in it. Example: Now, save the file and run the file using the following command in the command prompt. Syntax: If the program runs without raising any import error, the module is installed properly. Else it is recommended to reinstall the module and refer to its official documentation. Working with the Python dateutil moduleNow, let us proceed to work with the dateutil module in the Python programming language. The dateutil module and its subclassesThe dateutil module is split into several subclasses as shown below:
This module does not contain so many subclasses; however, we will only be exploring the functions of some of them in this tutorial. Importing the necessary modulesSince we have installed the module properly, now is the time to put the methods into action and retrieve the outputs. In order to start working with the Python dateutil module and some, we just have to import them first. The following syntax shows the importing of the required modules. Example: Explanation: In the above snippet of code, we have imported the datetime module to import methods. We have then imported some methods from the dateutil subclasses. The datetime functionalityBefore we get to the coding part of the dateutil module, we must understand that the module relies on the datetime module. The Python dateutil module works along with the datetime objects, and this implies that we will have to create the datetime objects before we start working with them. Thus, it becomes necessary for the importing of the datetime module. Now, let's begin working with some of the methods in the dateutil module. Datetime and relativedeltaThe relativedelta subclass extends off the datetime module, offering the programmers features to work with dates and times relative to the retrieved details. This statement signifies that we can insert days, months, or even years to the presently used datetime object. It also enables the programmers to work with intervals in time, with the datetime objects. Let us consider the following example to understand the concept properly. Example: Output: The Present datetime: 2021-10-20 12:38:19.319256 The Present date: 2021-10-20 Explanation: In the above snippet of code, we have imported the required modules and created the objects of the datetime module to retrieve details for present date and time. We have then printed those details for the users. Now, let us work with getting information with the help of the relative dates. Example: Output: Next month: 2021-11-20 12:54:38.805725 Next month, and a week: 2021-11-27 12:54:38.805725 Next month, plus one week, at 4 PM: 2021-11-27 16:54:38.805725 Next Friday: 2021-10-22 Explanation: In the above snippet of code, we have imported the required modules and used the values of present date and time to find the information in respect to the relative deltas like date and time of next month, a month and a week, a month and a week at 4 PM and the date of upcoming Friday. A more applicable utilization of this module would be to find out the information with the help of some small operations. Let us consider the following example to understand the same. Example: Output: Last Monday: 2021-10-25 David's Age: 38 Explanation: In the above snippet of code, we have imported the required modules. We have then used the value of present date and time in order to find the information regarding any last day in the month or someone's age according to their birthday. Datetime and easterThe easter subclass is utilized to estimate date and time with generic easter calendars, allowing programmers to calculate the datetime objects with respect to a variety of calendars. The subclass is pretty small, and there is only one parameter with three options that define the whole module. The Julian Calendar, EASTER_JULIAN = 1 The Gregorian Calendar, EASTER_ORTHODOX = 2 The Western Calendar, EASTER_WESTERN = 3 Let us consider the following example to understand the working of the calendars as mentioned above. Example: Output: Julian Calendar: 1335-04-16 Gregorian Calendar: 1335-04-26 Western Calendar: 1335-04-17 Explanation: In the above snippet of code, we have imported the required modules and used the easter subclass to print the date in different Calendars formats. Datetime and parserThe parser subclass is used as a superior date and time string parser. This subclass is able to parse more than one known format depicting a date or a time. Let us consider the following example to understand the same. Example: Output: 2005-10-25 11:36:28 2005-10-25 11:36:28 2021-10-25 11:36:28 2021-10-23 11:36:00 Explanation: In the above snippet of code, we have imported the required modules again and used the parser subclass to parse the date and time strings. There are many choices we can offer, including the time zones, locally or explicitly. We can use the variables passed into the function as default parameters to extract details in order to deliver the time zones, year and time. Datetime and rruleThe rrule subclass is used to gather information regarding the recurrence of a datetime object and the datetime objects, respectively. Let us consider the following example to understand the same. Example: Output: [datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 9, 26, 9, 10), datetime.datetime(2021, 9, 27, 9, 10), datetime.datetime(2021, 9, 28, 9, 10), datetime.datetime(2021, 9, 29, 9, 10), datetime.datetime(2021, 9, 30, 9, 10), datetime.datetime(2021, 10, 1, 9, 10), datetime.datetime(2021, 10, 2, 9, 10), datetime.datetime(2021, 10, 3, 9, 10), datetime.datetime(2021, 10, 4, 9, 10), datetime.datetime(2021, 10, 5, 9, 10), datetime.datetime(2021, 10, 6, 9, 10), datetime.datetime(2021, 10, 7, 9, 10), datetime.datetime(2021, 10, 8, 9, 10), datetime.datetime(2021, 10, 9, 9, 10)] [datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 10, 3, 9, 10), datetime.datetime(2021, 10, 11, 9, 10), datetime.datetime(2021, 10, 19, 9, 10)] [datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 10, 2, 9, 10), datetime.datetime(2021, 10, 9, 9, 10), datetime.datetime(2021, 10, 16, 9, 10), datetime.datetime(2021, 10, 23, 9, 10)] [datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2021, 10, 25, 9, 10), datetime.datetime(2021, 11, 25, 9, 10), datetime.datetime(2021, 12, 25, 9, 10), datetime.datetime(2022, 1, 25, 9, 10)] [datetime.datetime(2021, 9, 25, 9, 10), datetime.datetime(2022, 9, 25, 9, 10), datetime.datetime(2023, 9, 25, 9, 10), datetime.datetime(2024, 9, 25, 9, 10), datetime.datetime(2025, 9, 25, 9, 10)] Explanation: In the above snippet of code, we have again imported the required module. We have then used the rrule subclass to collect information regarding the recurrence of a datetime object. A great ability of the dateutil module is that this subclass could allow programmers to work with multiple scheduling tasks and calendar storage innovations. Next TopicGetpass module in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India