Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare if a date is two months bigger than other with Python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading