How can i find if the date lies between two dates
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles
--
Chapters
00:00 How Can I Find If The Date Lies Between Two Dates
00:29 Accepted Answer Score 8
00:59 Answer 2 Score 2
01:23 Answer 3 Score 1
01:31 Thank you
--
Full question
https://stackoverflow.com/questions/1616...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles
--
Chapters
00:00 How Can I Find If The Date Lies Between Two Dates
00:29 Accepted Answer Score 8
00:59 Answer 2 Score 2
01:23 Answer 3 Score 1
01:31 Thank you
--
Full question
https://stackoverflow.com/questions/1616...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django
#avk47
ACCEPTED ANSWER
Score 8
Dates in the form YYYY-MM-DD can be compared alphabetically as well:
'2013-11-11' < '2013-11-15' < '2013-11-23'
date['min'] < your_date < date['max']
This won't work correctly for other formats, such as DD.MM.YYYY or MM/DD/YYYY. In that case you have to parse the strings and convert them into datetime objects.
If don't know whether the min/max variables are present, you can do:
date.get('min', '0000-00-00') < your_date < date.get('max', '9999-99-99')
and replace the default text values with anything you prefer.
ANSWER 2
Score 2
I think simple comparison works for that.
>>> from datetime import timedelta, date
>>> min_date = date.today()
>>> max_date = date.today() + timedelta(days=7)
>>> d1 = date.today() + timedelta(days=1)
>>> d2 = date.today() + timedelta(days=10)
>>> min_date < d1 < max_date
True
>>> min_date < d2 < max_date
False
Here is the updated version:
def is_in_range(d, min=date.min, max=date.max):
    if max:
        return min < d < max
    return min < d
print is_in_range(d1, min_date, max_date)
print is_in_range(d2, min_date, max_date)
print is_in_range(d1, min_date)
print is_in_range(d2, min_date)
True
False
True
True
ANSWER 3
Score 1
If you deal with date objects:
from datetime import date
in_range = (first_date or date.min) < my_date < (second_date or date.max)