When I add or subtract the datetime type, the timedelta type is returned, but it has only finer attributes than the day, and I always googled when calculating the month and year. So I will summarize it.
-Generate a new date by adjusting the date and the number of months (use relativedelta * timedelta cannot handle months) -Calculate the number of months and years of the difference between two dates (use monthmod)
#Create start date and time with datetime type-----------
from datetime import datetime
dt1 = datetime(2018,5,6) # datetime.datetime(2018, 5, 6, 0, 0)
#Generate a new date by adding any number of months and days to the start date and time-----------
from dateutil.relativedelta import relativedelta
dt2 = start + relativedelta(months=20, days=25) # datetime.datetime(2020, 1, 31, 0, 0)
#Find the difference between two dates on a daily basis-----------
dt_dif = (dt2 - dt1) # datetime.timedelta(days=635)
print(dt_dif.days) # 635
# print(dt_dif.months) #Will be an error
#The difference between the two dates, on a monthly basis/Calculate on a yearly basis-----------
from monthdelta import monthmod #If you get an error pip install MonthDelta
mmod = monthmod(dt1, dt2) # (monthdelta(20), datetime.timedelta(days=25)) <-Tuple (like a list)
##Month difference (round off the remainder)
print(mmod[0].months) # 20
##Year difference (round off the remainder) Divide the month difference by 12. There are various days in the month, but since the number of months in the year is always 12, this is ok
print(mmod[0].months//12) # 1
https://pythonhosted.org/MonthDelta/