To check for overlap in two different dateranges, {Start1, End1}
and {Start2, End2}
I am checking:
if ((Start1 <= End2) && (End1 >= Start2))
{
//overlap exists
}
The question is,
what is a good way to compare overlaps if I had let's say five dateranges?.
checking to see if any of them don't overlap each other?
If I have multiple date ranges, how to find if any of these ranges are overlapping?
To find if all are overlapping
static bool Overlap(params Tuple<DateTime, DateTime>[] ranges)
{
for (int i = 0; i < ranges.Length; i++)
{
for (int j = i + 1; j < ranges.Length; j++)
{
if (!(ranges[i].Item1 <= ranges[j].Item2 && ranges[i].Item2 >= ranges[j].Item1))
return false;
}
}
return true;
}
to find if any are overlapping
static bool Overlap(params Tuple<DateTime, DateTime>[] ranges)
{
for (int i = 0; i < ranges.Length; i++)
{
for (int j = i + 1; j < ranges.Length; j++)
{
if (ranges[i].Item1 <= ranges[j].Item2 && ranges[i].Item2 >= ranges[j].Item1)
return true;
}
}
return false;
}