# Date Range Overlap Testing

By , 2 Aug 2011

## Introduction

Before I wrote this class, I used to test for date range overlaps the hard way having to check each endpoint with lots of date comparisons. This way is much simpler and also more efficient.

## Background

With testing for overlaps on any range of numbers/dates, there are three possibilities. These are shown below.

1. Where the second interval (CD) overlaps on the beginning of the first (AB)
2. Where the second interval (EF) overlaps on the end of the first (AB)
3. Where the first interval (AB) exists inside the second (GH)

In the first case, you have to test if D lies between A and B. In the second, you have to test if E lies between A and B and in the third case, you have to test if both A and B lie between G and H. This leads to a lot of date comparisons to ensure you don't miss any case of an overlap.

There is a better way to test for overlaps using timespans which also enables you to test for more than two date ranges at a time. If you add up the total timespans of all the intervals AB, CD, EF and GH and compare this with the timespan of the minimum date (C) and maximum date (F), if the sum of the intervals is greater than the total timespan then there is an overlap, otherwise there is not.

In the image below, you can see that if you add up all the timespans of the intervals, this will be less than the total timespan and hence there is no overlap.

## Using the Code

I have included two functions in the `DateRange` class that perform the testing. The first only tests if there is an overlap at all and return `true `if there is. The second returns a list of which date range pairs have a conflict.

Here are the prototypes:

```/// <summary>
/// Tests for an overlap of given date ranges
/// </summary>
/// <param name="TestEndPoints">Set to true if you want to count the date range
/// end points as an overlap and false if you don't care if the end points overlap.
/// </param>
/// <param name="Ranges">The set of date ranges that you want to test.</param>
/// <returns>True if there is an overlap of the ranges. False if there is no overlap.
/// </returns>
public static bool HasOverlap(bool TestEndPoints, params DateRange[] Ranges); ```
```/// <summary>
/// Tests for an overlap of given date ranges
/// </summary>
/// <param name="TestEndPoints">Set to true if you want to count the date range
/// end points as an overlap and false if you don't care if the end points overlap.
/// </param>
/// <param name="Ranges">The set of date ranges that you want to test.</param>
/// <returns>A list of pairs of date ranges that overlap each other.
/// The list will be empty if there is no overlaps.</returns>
public static IEnumerable<OverlapPairs> OverlapingRanges
(bool TestEndPoints, params DateRange[] Ranges);```

The code snippet below will return `true`.

```DateRange.DateRange.HasOverlap(false,

Whereas this code snippet will return a list containing one Overlap Pairs of the first two date ranges. The third date range does not conflict at all.

```DateRange.DateRange.OverlapingRanges(false,

## History

• 28th July, 2011 - Original version posted
• 2nd August, 2011 - Updated code to account for whitespace in more than 2 date ranges

Software Developer (Senior) Purtill Bros Nominees Pty Ltd
Australia
Several years experience with a number of different languages including C/C++, Java, PHP, MS SQL, MySQL, VB.NET, C#.NET.

I have worked with Windows from 3.1 to 7, and NT 4 to Server 2008. I also have professional experience with Linux, mainly as Samba servers and minor experience with Asterisk phone system.

Most recently developing winforms programs for the current company I work for which operates in the transport industry.

In the process of becoming a CPA as well.

Votes of 3 or less require a comment

 Search this forum Profile popups    Spacing RelaxedCompactTight   Noise Very HighHighMediumLowVery Low   Layout Open AllThread ViewNo JavascriptPreview   Per page 102550
 First Prev Next
 My vote of 5 Arlen Navasartian 26-Apr-12 15:03
 A naive suggestion [modified] dproteau 2-Aug-11 7:27
 My vote of 1 PaulLinton 1-Aug-11 13:49
 Doesn't work PaulLinton 1-Aug-11 13:32
 Re: Doesn't work Mathew Crothers 1-Aug-11 18:00
 Simple approach Member 3860836 1-Aug-11 8:03
 Re: Simple approach Mathew Crothers 1-Aug-11 12:31
 Re: Simple approach pépé 2-Aug-11 5:04
 Alternate Approach: Timeline Jani Giannoudis 29-Jul-11 0:26
 Suggestion pépé 28-Jul-11 6:01
 Re: Suggestion Mathew Crothers 28-Jul-11 12:37
 Re: Suggestion pépé 28-Jul-11 21:40
 Last Visit: 31-Dec-99 18:00     Last Update: 19-Jun-13 21:56 Refresh 1