Reputation: 545
I am working on a booking system which works around the user selecting a time slot and I am stuck attempting to check for overlapping bookings.
I am attempting to create a function which returns all the available time slots. I have an array called bookings
which are the bookings for that day, and I have an array called slots
.
I have tried looping through the arrays and attempting this:
if (slot.start > booking.dateStart.clone().addMinutes(-30) && slot.end < booking.dateEnd.clone().addMinutes(30))
To check for any overlap, and if there is I splice the slot from the array. I need to leave a 30 minute gap between each booking so that is why I am adding minutes.
Upvotes: 2
Views: 2167
Reputation: 12815
The formula to find overlapping time periods is
start1 <= end2 && end1 >= start2
If that is true, then the periods do overlap. If you need to leave a 30 minute gap, just add that to the comparison of end1
and start2
.
Upvotes: 9