Alex Rivera | Logout

How check intersection of DateTime periods

Asked 2011-09-06T19:30:26.670
33

I have four DateTime objects. A1, A2 and B1, B2.

I need to know that the period A1-A2 doesn't intersect with period B1-B2. But I don`t want to write dirty code, like many if blocks.

if (A1 < B1 && A2 > B1)
{
    return false;
}

.... etc.

EDITED

I tried to use this one: Comparing ranges

DateTime A1 = DateTime.MinValue.AddMinutes(61);
DateTime A2 = DateTime.MinValue.AddHours(1.2);
DateTime B1 = DateTime.MinValue.AddMinutes(5);
DateTime B2 = DateTime.MinValue.AddHours(1);

Console.WriteLine(Range.Overlap(
    new Range<DateTime>(A1, A2),
    new Range<DateTime>(B1, B2)
));

It returned true but I expected false. Because this code always returns true

 if (left.Start.CompareTo(left.Start) == 0)
 {
     return true;
 }
Edit
Report

3 Answers

41

I dont believe there is going to be any manner of 'easy' code to write; you have to account for 4 distinct use cases. If you need to do this kind of check a lot, I'd write an extension method. Otherwise, you just need to check these conditions:

 |--- Date 1 ---|
      | --- Date 2 --- |


      | --- Date 1 --- |
 | --- Date 2 ---- |


 | -------- Date 1 -------- |
      | --- Date 2 --- |

      | --- Date 1 --- |
 | -------- Date 2 -------- |

EDIT: To provide actual code:

public class DateTimeRange
{
     public DateTime Start { get; set; }
     public DateTime End { get; set; }

     public bool Intersects(DateTimeRange test)
     {
         if(this.Start > this.End || test.Start > test.End)
            throw new InvalidDateRangeException();

         if(this.Start == this.End || test.Start == test.End)
              return false; // No actual date range

         if(this.Start == test.Start || this.End == test.End)
              return true; // If any set is the same time, then by default there must be some overlap. 

         if(this.Start < test.Start)
         {
              if(this.End > test.Start && this.End < test.End)
                  return true; // Condition 1

              if(this.End > test.End)
                  return true; // Condition 3
         }
         else
         {
              if(test.End > this.Start && test.End < this.End)
                  return true; // Condition 2

              if(test.End > this.End)
                  return true; // Condition 4
         }

         return false;
    }
}

That should cover the use cases.

answered 2011-09-06T19:34:26.157
19

My approach is to create a class called Period which contains Start and End properties (DateTime). This class can have methods or extension methods to calculate things like intersections. Let's say you have a method like this in your Period class:

public bool IntersectsWith(Period otherPeriod)
{
    return !(this.Start > otherPeriod.End || this.End < otherPeriod.Start);
}

Then you can write code like this:

if (!periodA.IntersectsWith(periodB))
{
    return false;
}
answered 2011-09-06T19:58:02.747
3

No way around it:

*Edited for simplification:

Assuming B2>B1 and A2>A1:

if (A2 >= B1 && A1 <= B2) {
    // some part of a1-a2 is in b1-b2
}

This will detect if any part of A1-A2 is in B1-B2.

If you need to detect if A1-A2 is completely in B1-B2:

if (B1 <= A1 && B2 >= A2) {
    // all of a1-a2 is in b1-b2
}
answered 2011-09-06T19:34:35.243

Your Answer