Click here to Skip to main content
Click here to Skip to main content

Date Range Overlap Testing

, 2 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A class to determine if there are any overlaps in a set of date ranges
Sample Image


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.


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.

	new DateRange.DateRange(DateTime.Now,DateTime.Now.AddDays(1)), 
	new DateRange.DateRange(DateTime.Now.AddDays(-1),DateTime.Now.AddDays(2)));

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.

	new DateRange.DateRange(DateTime.Now,DateTime.Now.AddDays(1)), 
	new DateRange.DateRange(DateTime.Now.AddDays(-1),DateTime.Now.AddDays(2)),
	new DateRange.DateRange(DateTime.Now.AddDays(-5),DateTime.Now.AddDays(-3)));


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mathew Crothers
Software Developer (Senior) Exclusive Software
Australia 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.

Comments and Discussions

GeneralMy vote of 5 PinmemberArlen Navasartian26-Apr-12 16:03 
SuggestionA naive suggestion [modified] Pinmemberdproteau2-Aug-11 8:27 
GeneralMy vote of 1 PinmemberPaulLinton1-Aug-11 14:49 
QuestionDoesn't work PinmemberPaulLinton1-Aug-11 14:32 
AnswerRe: Doesn't work PinmemberMathew Crothers1-Aug-11 19:00 
SuggestionSimple approach PinmemberMember 38608361-Aug-11 9:03 
GeneralRe: Simple approach PinmemberMathew Crothers1-Aug-11 13:31 
GeneralRe: Simple approach Pinmemberpépé2-Aug-11 6:04 
SuggestionAlternate Approach: Timeline PinmemberJani Giannoudis29-Jul-11 1:26 
SuggestionSuggestion Pinmemberpépé28-Jul-11 7:01 
GeneralRe: Suggestion PinmemberMathew Crothers28-Jul-11 13:37 
GeneralRe: Suggestion Pinmemberpépé28-Jul-11 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 2 Aug 2011
Article Copyright 2011 by Mathew Crothers
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid