Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been doing some overlapping ranges test and have successfully concluded the overlapping date ranges scenario, But on the other hand I have to check for the overlapping time ranges, and I am unable to perform that. I have a scenario as below,

I have a range of 8am to 5pm and second range of 8 pm to 9am

I have tried like 3

C#
bool overlap = a.start < b.end && b.start < a.end;


And the expected results are true as 8am overlapps in the 8pm to 9am range, but because of the date part associated, it causing the trouble.


Any ideas or guide to solve this?

What I have tried:

C#
bool overlap = a.start < b.end && b.start < a.end;
Posted
Updated 3-Nov-16 2:36am

Try this:

C#
bool overlap = a.start.TimeOfDay < b.end.TimeOfDay && b.start.TimeOfDay < a.end.TimeOfDay;
 
Share this answer
 
Comments
VICK 3-Nov-16 6:55am    
Posted solution only works when the time range also includes the date part as well. Or the range is linear i.e. not the cross day.
I asume a.start and a.end are DateTime/DateTimeOffset instances.
Use the a.start.TimeOfDay property from your DateTimes to get an TimeSpan from the dates (to ignore the date part in the calculation)

like this:
C#
bool overlap = a.start.TimeOfDay < b.end.TimeOfDay  && b.start.TimeOfDay  < a.end.TimeOfDay;


or just store the times in the first place e.g like this then your comparison would also work

C#
DateTime myTime = default(DateTime).Add(myDateTime.TimeOfDay);
 
Share this answer
 
Comments
VICK 3-Nov-16 6:55am    
Posted solution only works when the time range also includes the date part as well. Or the range is linear i.e. not the cross day.
johannesnestler 3-Nov-16 7:56am    
I don't get what you are trying to tell... we compare times only here - what you mean with "date part". You asked how to check time-overlaps...
VICK 3-Nov-16 8:37am    
Please have a look at my solution posted, In which I have tried to explain the exact issue I was facing and the solution I made to it.
Suppose we Have a date range a which start and end time are as below

C#
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,

C#
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.

C#
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.
 
Share this answer
 
Comments
phil.o 3-Nov-16 8:49am    
I had to set this up once, this is harldly something that can be answered quickly; I'm afraid. I remember I created a Range structure, and that there were several ways two ranges could overlap or not. So the AreOverlapping result would use a combination of several of these ways.
I also remember that
Jani Giannoudis
[^]'s article Time Period Library for .NET[^] was of great help for me to understand and model the process.
Unfortunately I do not have that code anymore. That was for a MSCRM version I was so happy to stop working with.
VICK 3-Nov-16 8:54am    
Actually I didn't get any method from the TimePeriodLibrary regarding TimeRange issue without comparison of the date parts. Or else I was unable to understand it in depth.
Would be pretty happy If you could make a code snippet using that Library as per my above mentioned scenario?
phil.o 3-Nov-16 9:06am    
As I told you, what I had to set up was not a "quick snippet" solution, and I'm afraid this won't fit a Quick Answers forum. You cannot move forward if you think you do not need to compare with the date part. Date part is as important as the time part if the periods you want to compare overlap on one or several day boundaries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900