My goal is to add a specified amount of months to a start_date based on the length of the time period between start_date and end_date. If the period lasted more than 10 years, 3 months should be added, otherwise 1 month. The calculation has to be precise and should be able to account for the specific numbers of days in a given month, as well as for leap years. I tried to accomplish this by using dateutil.relativedelta like so:
from dateutil.relativedelta import relativedelta
from dateutil import parser
# Extract date from string
start_date = parser.parse("2050-11-01")
end_date = parser.parse("1980-11-01")
# Calculate difference
delta = relativedelta(start_date, end_date)
print(delta)
if delta > relativedelta(years=+10):
new_end_date = start_date + relativedelta(months=3)
else:
new_end_date = start_date + relativedelta(months=1)
print(new_end_date)
However, this gives the following output:
relativedelta(years=+70)
TypeError: '>' not supported between instances of 'relativedelta' and 'relativedelta'
Searching for the error on SO made it clear that there is no simple way to compare dateutil.relativedelta. Does anyone know of a work-around for my use case?