How to tell if a date is between two other dates? How to tell if a date is between two other dates? python python

How to tell if a date is between two other dates?


If you convert all your dates to datetime.date, you can write the following:

if start <= date <= end:    print("in between")else:    print("No!")


As you are still not satisfied, I have another answer for you. Without using datetime and year.

It just uses built-in tuples and comparing them:

d1 = (3, 28)d2 = (3, 31)d3 = (4, 2)if d1 < d2 < d3:    print("BETWEEN!")else:    print("NOT!")

You can create tuple like these easily:

day = 16month = 4d = (month, day)


Use datetime.date:

http://docs.python.org/library/datetime.html#datetime.date

< operator is overloaded specially for you.

date1 < date2 - date1 is considered less than date2 when date1 precedes date2 in time.

>>> from datetime import date>>> d1 = date(2011, 3, 28)>>> d2 = date(2011, 3, 22)>>> d3 = date(2011, 4, 3)>>> d2 < d1 < d3True

Or in your prgram:

from datetime import dated1 = date(2011, 3, 28)d2 = date(2011, 3, 22)d3 = date(2011, 4, 3)if d2 < d1 < d3:    print('in between')else:    print('No!')