The main features of the dateutil module

by Alex
The main features of the dateutil module

When working with scripts that need to get data in real time, I recommend using the dateutil module. It helps you schedule, get data at a specific time or set timestamps. Handling date and time with the built-in datetime module in a large project is not an easy task. Considering the number of changes that need to be applied when getting “raw” data. The dateutil module was created for this purpose. It provides extensions to methods already available in datetime.

Installing the dateutil module in Python

Before you start working with the dateutil module, you need to install it using the following line:

pip install python-dateutil

This example uses the pip package manager, but nothing prevents you from using Anaconda.

Working with the module

After the installation you can start working with the module itself. Dateutil is divided into several subclasses:

  • easter,
  • parser,
  • parser,
  • rrule,
  • tz
  • and some others.

There aren’t many of them, but in this material we’ll break down just a few of them.

Importing the right methods

After installing the module, you need to select the required methods. At the top of the code, the required elements must first be imported.


# you need to import the datetime module
from datetime import datetime, date
from dateutil.relativedelta import relativedelta, FR, TU
from dateutil.easter import easter
from dateutil.parser import parse
from dateutil import rrule

This will allow you to use them to run the examples.

Datetime and relativedata

So, dateutil depends on the datetime module. It uses its objects. The relativedelta subclass extends the capabilities of the datetime module by providing functions for working with dates and times relative to the received data. This means you can add days, months, and years to the current datetime object. It is also possible to work with intervals.


# Creating multiple datetime objects to work with
dt_now = datetime.now()
print("Date and time right now:", dt_now)
dt_today = date.today()
print("date today:", dt_today)
Date and time right now: 2021-02-06 12:06:58.192574
Date today: 2021-02-06

Let’s try to work with the obtained information using relative dates.


# Next month
print(dt_now + relativedelta(months=+1))
# Next month, plus one week
print(dt_now + relativedelta(months=+1, weeks=+1))
# Next month, plus one week, at 5:00 p.m.
print(dt_now + relativedelta(months=+1, weeks=+1, hour=17))
# Next Friday
print(dt_today + relativedelta(weekday=4))
2021-03-06 12:11:53.648565
2021-03-13 12:11:53.648565
2021-03-13 17:11:53.648565
2021-02-12

With the help of basic operations you can get a variety of information.


# Find out the last Tuesday of the month
print(dt_today + relativedelta(day=31, weekday=TU(-1))
# We can also work directly with datetime objects
# Example: Determining age.
birthday = datetime(1965, 12, 2, 12, 46)
print("Age:", relativedelta(dt_now, birthday).years)

For example, in the example above it was possible to get years of object relativedelta.

2021-02-23
Age: 55

And this object can be manipulated in many different ways.

Datetime and easter

The easter subclass is used to calculate dates and times, taking into account different calendars. This subclass is quite compact and includes only one argument with three options:

  • Julian calendar EASTER_JULIAN=1.
  • Gregorian calendar EASTER_ORTHODOX=2.
  • Western calendar EASTER_WESTERN=3.

Here’s what it would look like in code:


print("Julian calendar:", easter(2021, 1))
print("Gregorian calendar:", easter(2021, 2))
print("Western calendar:", easter(2021, 3))
Julian calendar: 2021-04-19
Gregorian calendar: 2021-05-02
Western calendar: 2021-04-04

Datetime and parser

The parser subclass adds an advanced date and time parser, with which you can parse different date and time formats.


print(parse("Thu Sep 25 10:36:28 BRST 2003"))
# We can also ignore time zone
print(parse("Thu Sep 25 10:36:28 BRST 2003", ignoretz=True))
# We can ignore the time zone or year.
print(parse("Thu Sep 25 10:36:28"))
# We can prepend variables
default = datetime(2020, 12, 25)
print(parse("10:36", default=default))

It can be used with different explicit arguments, such as time zones. And with variables, different elements can be removed from the final result.

2003-09-25 10:36:28
2003-09-25 10:36:28
2021-09-25 10:36:28
2020-12-25 10:36:00

Datetime and rrule

The rrule subclass uses user input to provide information about the repeatability of the datetime object.


# Output 5 days from start date
print(list(rrule.rrule(rrule.DAILY, count=5, dtstart=parse("20201202T090000"))))
# Output 3 days from the start date at 10 intervals
print(list(rrule.rrule(rrule.DAILY, interval=10, count=3, dtstart=parse("20201202T090000"))))
# Output 3 days at an interval of a week
print(list(rrule.rrule(rrule.WEEKLY, count=3, dtstart=parse("20201202T090000"))))
# Print 3 days at a month interval
print(list(rrule.rrule(rrule.MONTHLY, count=3, dtstart=parse("20201202T090000"))))
# Print 3 days at a year interval
print(list(rrule.rrule(rrule.YEARLY, count=3, dtstart=parse("20201202T090000"))))

An important feature of the dateuitl module is the ability to work with scheduled tasks and calendar functions using this subclass.

[datetime.datetime(2020, 12, 2, 9, 0), datetime.datetime(2020, 12, 3, 9, 0), datetime.datetime(2020, 12, 4, 9, 0), datetime.datetime(2020, 12, 5, 9, 0), datetime.datetime(2020, 12, 6, 9, 0)]
[datetime.datetime(2020, 12, 2, 9, 0), datetime.datetime(2020, 12, 12, 9, 0), datetime.datetime(2020, 12, 22, 9, 0)]
[datetime.datetime(2020, 12, 2, 9, 0), datetime.datetime(2020, 12, 9, 9, 0), datetime.datetime(2020, 12, 16, 9, 0)]
[datetime.datetime(2020, 12, 2, 9, 0), datetime.datetime(2021, 1, 2, 9, 0), datetime.datetime(2021, 2, 2, 9, 0)]
[datetime.datetime(2020, 12, 2, 9, 0), datetime.datetime(2021, 12, 2, 9, 0), datetime.datetime(2022, 12, 2, 9, 0)]

Conclusions

Now you know that the dateutil module allows you to expand the amount of information obtained from datetime, allowing you to work with it more conveniently.

Related Posts

LEAVE A COMMENT