-3

I have Appointment objects made like this:

public Appointment(string p_Title, DateTime p_Start, TimeSpan p_Lenght)

I want to check if objects overlap but no matter what I try, I seem to get overlaps in every object that indicates the same day. I formated the DateTime to YYYY,MM,DD, HH:MM:SS. TimeSpan is HH:MM:SS

Servy
  • 202,030
  • 26
  • 332
  • 449
  • 1
    Can you expand on what you mean by overlap? Do you want to see if the two dates are equal? Or do you want to check if two objects are equal? – Steven Dec 07 '18 at 18:30
  • 3
    A timespan is just a length of time. It isn't a time of day. Use distance as an analogy. Our span is 50 feet, and yesterday I ran near a stop sign near my house. Did my route overlap with the span? –  Dec 07 '18 at 18:31
  • @Amy So what's the problem? The timespan is the length of the appointment, not the time of day that it happens. Saying I have a meeting at 2:30 p.m. tomorrow and it's an hour long is an entirely sensible statement. – Servy Dec 07 '18 at 18:35
  • 3
    *"no matter what I try"* ... what do those attempts look like? – madreflection Dec 07 '18 at 18:35
  • Hello, could you please include the code of one of your tries and an explanation of what your thought process was in writing the code? – Martin Boros Dec 07 '18 at 18:41
  • Hi Francois, and welcome to StackOverflow! Unfortunately, your question isn't clear enough for us to provide a helpful answer. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) in the help center. You can then close this question and ask a new one, if you don't find what you need from searching. Do note that there are already thousands of questions on datetime in c#, so you're likely to find this asked and answered already. Thanks! – Matt Johnson-Pint Dec 07 '18 at 18:41
  • @Servy `Saying I have a meeting at 2:30 p.m. tomorrow and it's an hour long is an entirely sensible statement` Sure, that is sensible, but the 2:30pm time does not overlap with an hour. That doesn't make sense. It does make sense if you ask if the 2:30 pm appointment, which is an hour long, overlaps with a range of time spanning 3:00pm to 3:30pm. It only makes sense to ask about overlap with specific ranges of time with well-defined start/stop times. If those times are not defined, asking about overlap is not sensible. –  Dec 07 '18 at 18:46
  • @Amy They didn't ask if the start of the appointment overlaps with its length. They asked how to check if different appointments overlap. – Servy Dec 07 '18 at 18:48
  • @Servy My point is that a timespan is not a time of day. It's a length of time. You need two dateTimes and two timespans to determine if there is any overlap. If I had a caterpillar on my desk and we observe its route, then we ask "does its route overlap with this ruler", the answer is not meaningful until we define the ruler's position as well. –  Dec 07 '18 at 18:50
  • 2
    @Amy Yes, and given that the question labels the timespan as `p_Lenght` and not `p_TimeOfDay`, I think it's pretty safe to say that the OP understands that a timespan is a length of time, not a time of day. What's your basis for asserting they think it's a time of day? They asked how to determine when appointments overlap, meaning you *would* have multiple sets of start times and lengths. – Servy Dec 07 '18 at 18:52
  • Both of you are correct. One thing that may help is that in C#, a `TimeSpan` is sometimes used in place of a time-of-day, since there is no built-in time-of-day type. For example, `DateTime.TimeOfDay` returns the time as a `TimeSpan`. This is discussed more in depth [here](https://stackoverflow.com/a/18648819/634824). – Matt Johnson-Pint Dec 07 '18 at 18:57
  • @MattJohnson But that's not how it's being used *here*, and is not a problem with this question. The assertion was made that the OP is somehow trying to compare times with times of day, and therefore the question doesn't make sense. They aren't trying to do that, they're asking something different. That some people use timespans to store times of day in other contexts is irrelevant at best. – Servy Dec 07 '18 at 18:59
  • It's too difficult to tell, due to not enough information in the question. However, I do think perhaps the problem is related to the last two sentences, which if I'm interpreting literally may simply be related to case sensitivity of the formatting characters chosen. (months instead of minutes, etc.) – Matt Johnson-Pint Dec 07 '18 at 19:04
  • `no matter what I try` Please show us what you tried. – mjwills Dec 09 '18 at 11:05

2 Answers2

2

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.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
1

This is how you do isOverlap() for a List in C#. isOverlap for proposed times is used with isValid(). For validation for a value or a set of values, you can use timeOfDay with it—or, at least, this is how I learned how to use it.

private bool isOverlap(DateTime newStart, DateTime newEnd, DateTime existingStart, DateTime existingEnd)
{
    return newStart < existingStart ? newEnd >= existingStart : newStart <= existingEnd;
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
TinaB
  • 11
  • 3