Checking date against date range in Python
Question:
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.
Answers:
Subtracting two date
objects gives you a timedelta
object, which you can compare to other timedelta
objects.
For example:
>>> from datetime import date, timedelta
>>> date(2011, 1, 15) - date.today()
datetime.timedelta(1)
>>> date(2011, 1, 15) - date.today() < timedelta(days = 3)
True
>>> date(2011, 1, 18) - date.today() < timedelta(days = 3)
False
As to “where to look”: the official documentation is excellent.
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
Others have already more than adequately answered, so no need to vote on this answer.
(Uses technique shown in Mark Byers’ answer; +1 to him).
import datetime as dt
def within_days_from_today(the_date, num_days=7):
'''
return True if date between today and `num_days` from today
return False otherwise
>>> today = dt.date.today()
>>> within_days_from_today(today - dt.timedelta(days=1), num_days=3)
False
>>> within_days_from_today(dt.date.today(), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=1), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=2), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=3), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=4), num_days=3)
False
'''
lower_limit = dt.date.today()
upper_limit = lower_limit + dt.timedelta(days=num_days)
if lower_limit <= the_date <= upper_limit:
return True
else:
return False
if __name__ == "__main__":
import doctest
doctest.testmod()
Object oriented solution
import datetime
class DatetimeRange:
def __init__(self, dt1, dt2):
self._dt1 = dt1
self._dt2 = dt2
def __contains__(self, dt):
return self._dt1 < dt < self._dt2
dt1 = datetime.datetime.now()
dt2 = dt1 + datetime.timedelta(days = 2)
test_true = dt1 + datetime.timedelta(days = 1)
test_false = dt1 + datetime.timedelta(days = 5)
test_true in DatetimeRange(dt1, dt2) #Returns True
test_false in DatetimeRange(dt1, dt2) #Returns False
convert elapsed time into days then apply a ternary operation to the days elapsed
current_dt=datetime.now()
feed_dt = datetime.now()-timedelta(days=3)
from_dt=datetime(year=feed_dt.year,month=feed_dt.month,day=feed_dt.day)
days_elapsed=(current_dt-from_dt).days
print(True if days_elapsed <=3 else False)
Output:
True
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.
Subtracting two date
objects gives you a timedelta
object, which you can compare to other timedelta
objects.
For example:
>>> from datetime import date, timedelta
>>> date(2011, 1, 15) - date.today()
datetime.timedelta(1)
>>> date(2011, 1, 15) - date.today() < timedelta(days = 3)
True
>>> date(2011, 1, 18) - date.today() < timedelta(days = 3)
False
As to “where to look”: the official documentation is excellent.
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
Others have already more than adequately answered, so no need to vote on this answer.
(Uses technique shown in Mark Byers’ answer; +1 to him).
import datetime as dt
def within_days_from_today(the_date, num_days=7):
'''
return True if date between today and `num_days` from today
return False otherwise
>>> today = dt.date.today()
>>> within_days_from_today(today - dt.timedelta(days=1), num_days=3)
False
>>> within_days_from_today(dt.date.today(), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=1), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=2), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=3), num_days=3)
True
>>> within_days_from_today(today + dt.timedelta(days=4), num_days=3)
False
'''
lower_limit = dt.date.today()
upper_limit = lower_limit + dt.timedelta(days=num_days)
if lower_limit <= the_date <= upper_limit:
return True
else:
return False
if __name__ == "__main__":
import doctest
doctest.testmod()
Object oriented solution
import datetime
class DatetimeRange:
def __init__(self, dt1, dt2):
self._dt1 = dt1
self._dt2 = dt2
def __contains__(self, dt):
return self._dt1 < dt < self._dt2
dt1 = datetime.datetime.now()
dt2 = dt1 + datetime.timedelta(days = 2)
test_true = dt1 + datetime.timedelta(days = 1)
test_false = dt1 + datetime.timedelta(days = 5)
test_true in DatetimeRange(dt1, dt2) #Returns True
test_false in DatetimeRange(dt1, dt2) #Returns False
convert elapsed time into days then apply a ternary operation to the days elapsed
current_dt=datetime.now()
feed_dt = datetime.now()-timedelta(days=3)
from_dt=datetime(year=feed_dt.year,month=feed_dt.month,day=feed_dt.day)
days_elapsed=(current_dt-from_dt).days
print(True if days_elapsed <=3 else False)
Output:
True