from dateutil.relativedelta import relativedelta
# 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
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
Countdown ( Hour : Minute : Seconds ) for new year and showing Message
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("Dayes:",dt3.days," Months:",dt3.months," Years:", dt3.years)
Output
Dayes: 16 Months: 3 Years: 25
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
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 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.
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)
new_date = today - relativedelta(years=2, weeks=1)
print(new_date)
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
23-07-2020 | |
Thanks for this tutorial was really helpful... |
26-11-2020 | |
Very helpful especially the rrule entry. |