Click here to Skip to main content
13,504,420 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked
Posted 28 Jul 2011

Date Range Overlap Testing

, 2 Aug 2011
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, Javascript.

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.

Have previously developed winforms programs for the current company I previously worked for which operates in the transport industry.

I have finished the foundation level of the CPA Australia qualifying me as an ASA.

Most recently I made the switch to Angular.js and Node.js developing single page web applications and am loving it. Working for myself in my own regional NSW development business.

You may also be interested in...


Comments and Discussions

Questionconditional date overlaping PHP & MySQL Pin
Member 1374582628-Mar-18 21:42
memberMember 1374582628-Mar-18 21:42 
GeneralMy vote of 5 Pin
Arlen Navasartian26-Apr-12 15:03
memberArlen Navasartian26-Apr-12 15:03 
SuggestionA naive suggestion [modified] Pin
dproteau2-Aug-11 7:27
memberdproteau2-Aug-11 7:27 
GeneralMy vote of 1 Pin
PaulLinton1-Aug-11 13:49
memberPaulLinton1-Aug-11 13:49 
QuestionDoesn't work Pin
PaulLinton1-Aug-11 13:32
memberPaulLinton1-Aug-11 13:32 
AnswerRe: Doesn't work Pin
Mathew Crothers1-Aug-11 18:00
memberMathew Crothers1-Aug-11 18:00 
SuggestionSimple approach Pin
Member 38608361-Aug-11 8:03
memberMember 38608361-Aug-11 8:03 
GeneralRe: Simple approach Pin
Mathew Crothers1-Aug-11 12:31
memberMathew Crothers1-Aug-11 12:31 
GeneralRe: Simple approach Pin
pépé2-Aug-11 5:04
memberpépé2-Aug-11 5:04 
SuggestionAlternate Approach: Timeline Pin
Jani Giannoudis29-Jul-11 0:26
memberJani Giannoudis29-Jul-11 0:26 
SuggestionSuggestion Pin
pépé28-Jul-11 6:01
memberpépé28-Jul-11 6:01 
GeneralRe: Suggestion Pin
Mathew Crothers28-Jul-11 12:37
memberMathew Crothers28-Jul-11 12:37 
GeneralRe: Suggestion Pin
pépé28-Jul-11 21:40
memberpépé28-Jul-11 21:40 

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

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

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