Suppose we Have a date range
a
which start and end time are as below
a.start = '1900-01-01 08:00:00.000'; i.e 8am
a.end = '1900-01-01 17:00:00.000'; i.e. 5pm
and we want to check it either it overlaps with the existing ranges or not.
suppose one of the previous range is
b
which have start and end time like below,
b.start = '1900-01-01 23:00:00.000'; i.e 11pm
b.end = '1900-01-01 09:00:00.000'; i.e. 9am
so b has a overlapping period of 8am to 9am with a, as the time slots are cross-day so I find the overlapping like below.
bool isOverLapping = false;
a = FirstRange;
double basehours = (a.end - a.start).TotalHours;
if (basehours <= 0)
{
basehours = basehours + 24;
}
foreach(var b in previousRanges)
{
double alpha1 = (b.start - a.start).TotalHours;
if (alpha1 <= 0)
{
alpha1 = alpha1 + 24;
}
double alpha2 = (b.end - a.start).TotalHours;
if (alpha2 <= 0)
{
alpha2 = alpha2 + 24;
}
if (basehours <= alpha1 && basehours <= alpha2)
{
isOverLapping = false;
}
else
{
isOverLapping = true;
}
}
I hope it will help someone who faces the issue like i did.