0

I have two sets of dates (4 dates total), let's call them sets A and B. I need to see if set B date range is found in any order in set A. Below is an example of how the dates would be found:

Set A:

Start          Stop
11/9/2017      11/10/2017  
11/16/2017     11/18/2017

Set B

Start          Stop
11/7/2017      11/11/2017   # should match the first one in set A
11/15/2017     11/16/2017   # Should match the second one in set A

I figured a set of < > 's would work, but I can't seem to get it to match what I am looking for.

I have tried the following:

start1 = datetime(2017,11,9)
stop2 = datetime(2017,11,10)
start2 = datetime(2017,11,7)
stop2 = datetime(2017,11,11)

start1 >= start2 or stop1 <= stop2

this yields true. But i feel that i may be missing some by having the or, in there.

  • 2
    Please give a [mcve], *"can't seem to get it to match"* isn't a particularly useful description of the problem. – jonrsharpe Apr 16 '18 at 15:19
  • 2
    Is this what you're looking for: [Determine Whether Two Date Ranges Overlap](https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap)? – pault Apr 16 '18 at 15:24
  • isnt this pretty much the answer below? it still doesnt seem to work for me though. Though this is what i want – bdrilling33 Apr 18 '18 at 16:18

1 Answers1

0

A very rudimentary example of comparing dates in Python 3:

set_a_start = datetime.datetime(2017, 1, 1)
set_a_end = datetime.datetime(2017, 12, 31)

set_b_start = datetime.datetime(2017, 4, 1)
set_b_end = datetime.datetime(2017, 4, 30)

if(set_a_start <= set_b_start and set_a_end >= set_b_end):
    print("Set B is within Set A")

Update: added "<=" for cases where both sets share the same start date (">=" for end dates)

Kate Hanahoe
  • 166
  • 8
  • I'm having a hard time wrapping my head around the `>` `<` with dates ha. so i have the following and it doesn't work. but if i add `>=` to it, it does. will this give me false positives with other combinations? i want to say no, but with dates, there's no telling, ha – bdrilling33 Apr 18 '18 at 03:34
  • so using what you have, if i have the following dates, it says its false: `set_a_start = datetime.datetime(2017, 1, 5)` `set_a_end = datetime.datetime(2017, 1, 6)` `set_b_start = datetime.datetime(2017, 1, 3)` `set_b_end = datetime.datetime(2017, 1, 6)` but if i swap the start and ends around, it says true. – bdrilling33 Apr 18 '18 at 16:12
  • Yea that makes sense, because with those dates, set B is not within set A - it's actually larger than set A – Kate Hanahoe Apr 19 '18 at 10:21