How to find gaps between multiple, variable DateRanges in C#?

  • Thread starter Thread starter TheSteveXYZ
  • Start date Start date
T

TheSteveXYZ

Guest
Hello, I'm doing an appointment finder, where you can input multiple users' appointments and it should find empty gaps in a list with multiple and variable DateRanges. The methods I wrote do unfortunately not work. How could I do this without a large library like TimePeriodLibrary.

This is the appointments collection:

List < Range > appointments = new List < Range > ();

// user 1
appointments.Add(new Range(new DateTime(2020, 8, 3, 0, 0, 0), new DateTime(2020, 8, 4 + 1, 0, 0, 0)));
appointments.Add(new Range(new DateTime(2020, 8, 7, 10, 0, 0), new DateTime(2020, 8, 7, 12, 0, 0)));

// user 2
appointments.Add(new Range(new DateTime(2020, 8, 3, 14, 0, 0), new DateTime(2020, 8, 3, 15, 30, 0)));
appointments.Add(new Range(new DateTime(2020, 8, 6, 0, 0, 0), new DateTime(2020, 8, 6 + 1, 0, 0, 0)));
appointments.Add(new Range(new DateTime(2020, 8, 6, 0, 0, 0), new DateTime(2020, 8, 6 + 1, 0, 0, 0)));

// user 3
appointments.Add(new Range(new DateTime(2020, 8, 3, 14, 0, 0), new DateTime(2020, 8, 3, 15, 30, 0)));
appointments.Add(new Range(new DateTime(2020, 8, 6, 0, 0, 0), new DateTime(2020, 8, 6 + 1, 0, 0, 0)));
appointments.Add(new Range(new DateTime(2020, 8, 6, 0, 0, 0), new DateTime(2020, 8, 6 + 1, 0, 0, 0)));

// ...


And this is my Range class:

class Range {
private readonly DateTime _start;
private readonly DateTime _end;

public Range(DateTime start, DateTime end) {
if (start.CompareTo(end) >= 0) {
var temp = end;
end = start;
start = temp;
}

_start = start;
_end = end;
}

public DateTime Start {
get {
return _start;
}
}

public DateTime End {
get {
return _end;
}
}
}

Thanks for any help :)

Continue reading...
 
Back
Top