I often find myself writing logic to compare two date ranges to see if they overlap. In fact, I’ve done this enough times that I thought it was worth capturing in a blog post to save myself the time of having to figure it out next time. Hopefully you, dear reader, will also find it useful.

### TL;DR

If you don’t need to understand the reason why, no need to read on. Just use this logic:

1 |
(StartA <= EndB) and (StartB <= EndA) |

### The Details

There are two different conditions that could make for non-overlapping ranges:

**Condition 1:**

Range B ends before Range A starts, which we could write as:

1 |
StartA > EndB |

**Condition 2:**

Range A ends before Range B starts, which we could write as:

1 |
StartB > EndA |

So, two ranges overlap if neither Condition 1 or Condition 2 is true, which we can write like this:

1 |
NOT ((StartA > EndB) OR (StartB > EndA)) |

Now, we know that “not (A or B)” is the same as “(not A) and (not B)”, so to simplify, we can write the above as:

1 |
(NOT (StartA > EndB)) and (NOT (StartB > EndA)) |

Finally, we can remove the NOTs by reversing the < and > comparisons, where, for example **NOT >** is the same as <**=**.

1 |
(StartA <= EndB) and (StartB <= EndA) |

## Web Hosting

Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges overlap?

## Web Hosting

I think the whole endpoint thing is just confusion about whether the bounds are inclusive or exclusive. For example, when two intervals are the exact same length even if not coincident or even overlapping, should that be considered another relation ?