I have a date variable: 2011-01-15
and I would like to get a boolean back if said date is within 3 days from TODAY. Im not quite sure how to construct this in Python. Im only dealing with date, not datetime.
My working example is a “grace period”. A user logs into my site and if the grace period is within 3 days of today, additional scripts, etc. are omitted for that user.
I know you can do some fancy/complex things in Python’s date module(s) but Im not sure where to look.
Advertisement
Answer
In Python to check a range you can use a <= x <= b
:
>>> import datetime >>> today = datetime.date.today() >>> margin = datetime.timedelta(days = 3) >>> today - margin <= datetime.date(2011, 1, 15) <= today + margin True