Test if a period overlaps

Shows how to test if two given time periods overlap each other.

/// <summary>
/// Tests if two given periods overlap each other.
/// </summary>
/// <param name="BS">Base period start</param>
/// <param name="BE">Base period end</param>
/// <param name="TS">Test period start</param>
/// <param name="TE">Test period end</param>
/// <returns>
/// 	<c>true</c> if the periods overlap; otherwise, <c>false</c>.
/// </returns>
public bool TimePeriodOverlap(DateTime BS, DateTime BE, DateTime TS, DateTime TE)
{
    // More simple?
    // return !((TS < BS && TE < BS) || (TS > BE && TE > BE));
 
    // The version below, without comments 
    /*
    return (
        (TS >= BS && TS < BE) || (TE <= BE && TE > BS) || (TS <= BS && TE >= BE)
    );
    */
 
    return (
        // 1. Case:
        //
        //       TS-------TE
        //    BS------BE 
        //
        // TS is after BS but before BE
        (TS >= BS && TS < BE)
        || // or
 
        // 2. Case
        //
        //    TS-------TE
        //        BS---------BE
        //
        // TE is before BE but after BS
        (TE <= BE && TE > BS)
        || // or
 
        // 3. Case
        //
        //  TS----------TE
        //     BS----BE
        //
        // TS is before BS and TE is after BE
        (TS <= BS && TE >= BE)
    );
}
Snippet Details



// Period A (Start & End date)
DateTime DateA_Start = new DateTime(2007, 8, 10); // Aug 10, 2007
DateTime DateA_End = new DateTime(2007, 9, 10);  // Sept 10, 2007
 
// Period B (Start & End date)
DateTime DateB_Start = new DateTime(2007, 9, 1); // Sept 1, 2007
DateTime DateB_End = new DateTime(2007, 12, 1);  // Dec 1, 2007
 
// Test if the given periods overlap
if (TimePeriodOverlaps(DateA_Start, DateA_End, DateB_Start, DateB_End))
{
    // This will be the result for the example values
    MessageBox.Show("The two periods overlap!");
}
else {
    MessageBox.Show("Everything is okay!");
}

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

Jani March 19, 2011 at 17:03
Check my 'http://www.codeproject.com/KB/datetime/TimePeriod.aspx' on
http://www.codeproject.com/KB/datetime/TimePeriod.aspx.

Cheers, Jani.
Jani March 19, 2011 at 17:02
Check out my 'Time Period Library for .NET' on http://www.codeproject.com/KB/datetime/TimePeriod.aspx.

Cheers, Jani.
Brian October 07, 2009 at 19:55
Thanks for this snippet. It was monumentally useful (and quick).
HenryGab July 28, 2009 at 02:14
This presumes BS < BE and that TS < TE, which means you expect your callers to call you properly.
Better would be to use DateTime baseStart, TimeSpan baseSpan, DateTime testStart, TimeSpan testSpan. This at least reduces improper usage.

Then, although timespan can still be negative (and still require more complex analysis), the probability of being improperly called is greatly reduced.
Sumanth April 08, 2009 at 21:26
// More simple?
// return !((TS < BS && TE < BS) || (TS > BE && TE > BE));

TE < BS => TS < BS since TS < TE.
So, you can just say
return !((TE < BS) || (TS > BE));
Dreamchaser November 13, 2008 at 10:08
This can be done much easier:

return BE > TS && TE > BS