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
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("Dayes:",dt3.days," Months:",dt3.months," Years:", dt3.years)
Output
Today: 2019-09-21 
No of days left for New year
Dayes: 11  Months: 3  Years: 0

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)  # 2019-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.

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
Your Rating




Post your comments , suggestion , error , requirements etc here




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