Checking date against date range in Python

73,594

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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()
Share:
73,594
Ben Keating
Author by

Ben Keating

A designer who loves to touch code and hardware.

Updated on July 13, 2021

Comments

  • Ben Keating
    Ben Keating almost 3 years

    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.