65

I have the following codes:

if date in (start, end):
        print('in between')
else:
        print('No!')

date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2, start as 3/14 and end as 11/7 and it's print 'No!', which means it's not running right. I guess have to format them to a date format and then compare them.

Shedrack
  • 656
  • 7
  • 22
widget
  • 945
  • 3
  • 13
  • 22
  • 1
    What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about `<` and `>` in a tutorial yet? – S.Lott Mar 28 '11 at 20:03
  • i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare? – widget Mar 28 '11 at 20:23
  • @widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No. – S.Lott Mar 28 '11 at 20:36
  • Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works. – Maciej Ziarko Mar 28 '11 at 20:44
  • @S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3 – widget Mar 28 '11 at 20:50
  • @widget. Are you asking how to build `datetime` objects from "records"? That seems to be a new and different question. Step 1. Search for "datetime parsing". Step 2. Read the `datetime` library in the Python docs. Step 3. If you cannot find anything then ask a separate question. – S.Lott Mar 28 '11 at 20:52
  • related: [Python - Working out if time now is between two times](http://stackoverflow.com/q/20518122/4279) – jfs Dec 10 '15 at 23:19

4 Answers4

143

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

if start <= date <= end:
    print("in between")
else:
    print("No!")
Shedrack
  • 656
  • 7
  • 22
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
55

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 = 16
month = 4
d = (month, day)
Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69
  • I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much! – widget Mar 28 '11 at 21:12
  • Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way. – Maciej Ziarko Mar 28 '11 at 21:18
  • I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient – widget Mar 29 '11 at 17:09
  • 2
    Note that it can be used only for dates of one year! – Demian Wolf May 29 '20 at 19:40
39

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 < d3
True

Or in your prgram:

from datetime import date

d1 = date(2011, 3, 28)
d2 = date(2011, 3, 22)
d3 = date(2011, 4, 3)

if d2 < d1 < d3:
    print('in between')
else:
    print('No!')
Shedrack
  • 656
  • 7
  • 22
Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69
3
from datetime import datetime

date_format = "%m/%d/%Y"

a = datetime.strptime('8/18/2008', date_format)

b = datetime.strptime('9/26/2007', date_format) # Date to be checked

c = datetime.strptime('9/25/2008', date_format)

d = datetime.strptime('8/18/2008', date_format)  #Date entered here should always be the same as 'a'

delta1 = b - a

delta2 = c - b

delta3 = d - a

if delta1.days >= delta3.days and delta2.days >= delta3.days:

    print('In between')

else:

    print('Not in between')
Shedrack
  • 656
  • 7
  • 22