Home > Software design >  Compare if a date is two months bigger than other with Python
Compare if a date is two months bigger than other with Python

Time:03-23

I've been trying for a while use python to compare if a date is two months bigger than other date.

Has anyone an idea of what I'm doing wrong?

Thanks in advance

import datetime
from datetime import datetime, timedelta
import time

dateEfec = "01/01/2016"
dateBase = "01/03/2016"

effectivedate = time.strptime(dateEfec,"%d/%m/%Y")
baseline = time.strptime(dateBase, "%d/%m/%Y")

calc = effectivedate > baseline   relativedelta(months=2)
print(calc)

I'm getting this error:

TypeError: can only concatenate tuple (not "relativedelta") to tuple

CodePudding user response:

timedelta has no attribute 'month' - because a month is an ambiguous quantity, it can have 28-31 days. Use relativedelta instead.

from datetime import datetime
from dateutil import relativedelta

dateEfec = "01/03/2016"
dateBase = "01/01/2016"

effectivedate = datetime.strptime(dateEfec,"%d/%m/%Y")
baseline = datetime.strptime(dateBase, "%d/%m/%Y")

calc = effectivedate >= baseline   relativedelta.relativedelta(months=2)
print(calc)
# True

Note: I've modified the example so it makes for a better illustration.

  • Related