Tuesday, January 12, 2021

Python Pool: Python dateutil Module: Explanation and Examples

Hello coders!! In this article, we will understand the python dateutil module in detail. Modules in Python are files that contain certain statements and definitions. These modules are used to break down a large program into small parts. They are imported into programs to avail their functions. Dateutil is one such predefined module in python used when we need to do certain date and time-related work. Without wasting any time, let’s get straight into the topic.

What is the dateutil module in Python?

There is a built-in module available in python called the datetime module. We use this module for the manipulation of dates and times from simpler to complex ways. Even though this module is sufficient for a certain number of instances, the dateutil module in python provides certain powerful extensions to the datetime module.

Key features of the dateutil Module in Python:

  • Parsing of dates in any string format
  • Internal up-to-date world timezone information
  • Computation of relative deltas
  • Computation of dates based on very flexible recurrence rules

Installation of Python dateutil Module:

Like any other module, dateutil can be installed from PyPI using pip.

pip install python-dateutil

Now that we have successfully installed the dateutil module let us explore some of its features with the help of certain examples.

Let us first import the required modules:

from datetime import *
from dateutil.relativedelta import *
import calendar

Example 1: To get the current date & time in Python

current = datetime.now()
print("Date\t   Time")
print(current)

Output & Explanation:

To get current date & time in Python using dateutilOutput
  • Syntax: 
    • datetime.now(tz)
  • Parameters:
    • tz: Specified time zone. By default – Greenwich Meridian time
  • Return Value: current date and time

In this code, we used the datetime.now() method to get the current date and the time. By default, the function returns the Greenwich Meridian time.

Example 2: To get the date of next week or next month

month_next = date.today() + relativedelta(months=+1)
print('Date of next month:')
print(month_next)
week_next = date.today() + relativedelta(weeks=+1)
print('Date of next week:')
print(week_next)

Output & Explanation:

To get the date of next week or next month using dateutilOutput
  • Syntax: 
    date.today()
  • Return Value: 
    current local date

Here, we used the relativedelta method to get the date of next month and next week. As we can see, when the month value is increased by one, the date generated is of the next month. Similarly, we have obtained the date of next week as well.

Example 3: Combining the dates(next month plus one week)

combined = date.today() + relativedelta(months=+1, weeks=+1)
print('Date of next moht and one week:')
print(combined)

Output & Explanation:

Combining the dates(next month plus one week)Output

For this example, we wanted to obtain the date of next month’s next week. So, we incremented the value of both months and weeks by 1. As a result, we got the date of next month and a week.

Example 4: Going backward Using relativedelta

month_last = date.today() + relativedelta(months=-1)
print('Date of last month:')
print(month_last)
week_last = date.today() + relativedelta(weeks=-1)
print('Date of last week:')
print(week_last)

Output & Explanation:

Going backward Using relativedeltaOutput

Just like we got the date of months ahead of us, we can also go back to get the date of month or week before the current one. We need to do that instead of increasing. We decreased the value of the relativedelta method to go backward.

Example 5: Getting a particular weekday Using relativedelta() in Python

friday= date.today() + relativedelta(weekday=FR)
print(friday)

Output & Explanation:

Getting a particular weekday Using relativedelta() in PythonOutput

If someone wants to get the date of a particular weekday in the given week, that is also possible. All they have to do is to specify the day, and the corresponding date is generated.

Example 6: Get difference between two dates

dob = datetime(1999, 5, 15)
my_age = relativedelta(date.today(), dob)
print(my_age)

Output & Explanation:

Get difference between two dates in pythonOutput

In this example, we have used the the feature of dateutil module where we can get the difference between two dates. So we entered a particular date of birth and used this property of the relative delta method to get the current age.

You might be also interested in reading:

Conclusion: dateutil module in python

These are the various examples showcasing the relative delta computation in python.

The post Python dateutil Module: Explanation and Examples appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...