简体   繁体   中英

Adding months to a date based on a comparison where dateutil.relativedelta is greater than a specified amount of time

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?

Here's my suggestion:

  • Set the (relative) period you are testing against (10 years)
  • Add it to the earliest input date
  • Check if that new date is earlier than the latest input date

This way, you still get the relative-ness of the relativedelta , but you get two hard dates to compare:

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")

# Set the size of the period
period = relativedelta(years=10)

# Test if adding the period to the earliest date makes it surpass the latest date
if min(start_date, end_date) + period <= max(start_date, end_date):
    new_end_date = start_date + relativedelta(months=3)
else:
   new_end_date = start_date + relativedelta(months=1)

print(new_end_date)
# 2051-02-01 00:00:00

In this case, start & end do not imply earlier & later, so you have to do the min and max operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM