Python relativedelta

Importing datetime library

from dateutil.relativedelta import relativedelta

Watch difference between day and days


# day and days
from dateutil.relativedelta import relativedelta
from datetime import date

dt = date.today()  # today is 2019-09-23

# Adding one day to today
print(dt + relativedelta(days=1))  # 2019-09-24

# 1st day of the month
print(dt + relativedelta(day=1))  # 2019-09-01
relativedelta(year,month,day, hour,minute, second, microsecond)
Note : Here all arguments are singular, we will check by using both. Here is the code.
from dateutil.relativedelta import relativedelta
dt = relativedelta(year=2021, month=1, day=1,hour=23,minute=56,second=57, microsecond=324534)
print(dt)
print(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond)
Output
relativedelta(year=2021, month=1, day=1, hour=23, minute=56, second=57, microsecond=324534)
2021 1 1 23 56 57 324534
We will use plurals now. This will add the arguments to our date object.
from dateutil.relativedelta import relativedelta

dt = relativedelta(years=1, months=2, days=3, hours=23, minutes=56, 
    seconds=57, microseconds=324534)

from datetime import date
dt_today = date.today()  # today is 2019-09-23
print(dt_today + dt)
Output
2020-11-26 23:56:57.324534
Using datetime ( adding to current date and time )
from dateutil.relativedelta import relativedelta

dt = relativedelta(
    years=1, 
    months=2, 
    days=3, 
    hours=23, 
    minutes=56, 
    seconds=57, 
    microseconds=324534
)

from datetime import datetime

dt_now = datetime.now()

print(dt_now)
print(dt_now + dt)
Output
2019-09-23 14:48:35.124959
2020-11-27 14:45:32.449493
Here are some common uses of relativedelta to find out date and time remaining or to apply any datetime calculation

Number of Month and days left for New Year
from dateutil.relativedelta import relativedelta
from datetime import date

dt = date.today()

print("Today:", dt)

print("No of days left for New year")

dt2 = date(dt.year + 1, 1, 1)

dt3 = relativedelta(dt2, dt)

print(
    "Days:", dt3.days, 
    "Months:", dt3.months, 
    "Years:", dt3.years
)
Output
Today: 2019-09-21
No of days left for New year
Days: 11  
Months: 3  
Years: 0
Countdown ( Hour : Minute : Seconds ) for new year and showing Message

Age Calculation

Input date of birth in Year, Month , day format. Output saying year month and days.
from dateutil.relativedelta import relativedelta
from datetime import date

dt = date.today()
dob = date(1996, 5, 15)  # date of birth as Year, month, day

dt3 = relativedelta(dt, dob)

print(
    "Days:", dt3.days, 
    "Months:", dt3.months, 
    "Years:", dt3.years
)
Output
Days: 16  Months: 3  Years: 25 

Adding a month to any date

We will findout difference between relativedelta and timedelta
Let us take the 31st of Jan 2019 as our date and we will add one month to this date.

By using month part of date object
from datetime import date

print("Adding 1 month to 31st of Jan")

dt1 = date(2019, 1, 31)

dt3 = date(
    dt1.year, 
    dt1.month + 1, 
    dt1.day
)  # ValueError generated

print(dt3)
This will generate ValueError and say day is out of range for month

By using timedelta
As there is no Month attribute for our timedelta, we can add 30 days
from datetime import timedelta, date, datetime

dt1 = date(2019, 1, 31)
dt3 = dt1 + timedelta(days=30)
print(dt3)  # 2019-03-02

dt1 = date(2020, 1, 31)
dt3 = dt1 + timedelta(days=30)
print(dt3)  # 2020-03-01
However you can see it takes care of leap year
By using relativedelta
from dateutil.relativedelta import relativedelta
from datetime import date

dt1 = date(2019, 1, 31)
dt3 = dt1 + relativedelta(months=1)
print(dt3)  # 2019-02-28

dt1 = date(2020, 1, 31)  # Leap year
dt3 = dt1 + relativedelta(months=1)
print(dt3)  # 2020-02-29
This gives us the last day of Feb month and at the same time takes care of leap year. relativedelta is usefull in handling adding or subtracting months , years etc.

Example 2: Adding Months and Days

from datetime import datetime
from dateutil.relativedelta import relativedelta

today = datetime.now()  # today's date used

new_date = today + relativedelta(months=3, days=5)

print(new_date)

Example 3: Subtracting Years and Weeks

new_date = today - relativedelta(years=2, weeks=1)
print(new_date)

Adding sequence

relativedelta is added to a datetime in the following sequence.
  1. Year
  2. Month
  3. Day
  4. Hours
  5. Minutes
  6. Seconds
  7. Microsecond
After adding the relativedelta we will apply the weekdays.
from dateutil.relativedelta import relativedelta
from datetime import date
from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU

dt = date.today()  # today is 2019-09-22

# 2nd Monday from today
print(dt + relativedelta(weekday=MO(2)))  # 2019-09-30

# 2nd Monday of present Month
print(dt + relativedelta(day=1, weekday=MO(2)))  # 2019-09-09

# Next Sunday from today
print(dt + relativedelta(weekday=SU(+1)))  # 2019-09-22

# As today is Sunday there is no difference with SU(+1) or SU(-1)
print(dt + relativedelta(weekday=SU(-1)))  # 2019-09-22

# Previous 2nd Tuesday
print(dt + relativedelta(weekday=TU(-2)))  # 2019-09-10


All Date Objects All timedelta objects
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    23-07-2020

    Thanks for this tutorial was really helpful...

    26-11-2020

    Very helpful especially the rrule entry.




    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer