0

I am trying to check if one datetime lies between two datetimes, most of the cases return true, but in below case it return false whereas it should return true, how can I check it in proper way.

time1=1900-01-01 08:30:00
time2=1900-01-01 00:00:00
inbetween=1900-01-01 20:00:00

    if (time1<=inbetween<=time2):  # if 12 hour format (8:30AM<8:00PM<12:AM) it should be true
        print("True")
        return True
    else:
        print("False")
        return False

in above case inbetween lies between time1 and time 2 so it should return True but it return False. How can I check. NOTE: the date will always remain same so I have to check it on the time basis.

Nabeel Ayub
  • 1,060
  • 3
  • 15
  • 35
  • 3
    ...`inbetween` is after `time1` and `time2` – Ollie Oct 31 '19 at 07:16
  • `time2 is 12AM of 1st` and `time1 is 8.30AM of 1st` but `inbetween is 8PM of 1st`. So its False only – Exprator Oct 31 '19 at 07:19
  • 3
    if time2 is 12:00 AM then time2 date should be like **"1900-01-02 00:00:00"**, I assume that you are searching **"1900-01-01 20:00:00"** in between **"1900-01-01 08:30:00"** and **"1900-01-02 00:00:00"**. At this Case it should return **"True"** – Narendra Lucky Oct 31 '19 at 07:28
  • in time2 you should use `02` as day or `23:59:59` as time. – furas Oct 31 '19 at 09:24

1 Answers1

0

I hope this help:

In your implementation, what happening is that inbetween come between time2 and time1, not time1 and time2. To fix this, you can set, time2 to 1900-01-01 23:59:59 or the next day.