I have Appointment
objects made like this:
public Appointment(string p_Title, DateTime p_Start, TimeSpan p_Lenght)
So each appointment has a start which is a DateTime
, and a length which is a TimeSpan
.
From these two parameters you could easily calculate the end of the appointment by adding the p_Length
value to the p_Start
value, using the Add
method of DateTime
, like this:
var end = p_Start.Add(p_Length);
Once you've calculated that, you have two DateTime
values for each appointment, so it's easy to calculate if two appointments overlap - you simply test if one starts before the other ends, while the other starts before the one ends (Please note that this is correct regardless of the data type being tested, see the overlap tag info for details) - like this:
bool AreOverlapping(Appointment a, Appointment b)
{
return a.Start < b.Start.Add(b.Length) &&
b.Start < a.Start.Add(a.Length)
}
If you consider an appointment that ends at 13:00 as an overlap of another appointment that starts at 13:00, simply change <
to <=
.
I formated the DateTime to YYYY,MM,DD, HH:MM:SS. TimeSpan is HH:MM:SS
Nor a DateTime
neither a TimeSpan
stored display format. Only string representations of them do - and to check for overlapping appointments you don't need to care about how this data is represented as a string - you just care about the data itself.