I have the following time ranges 1pm to 3pm and 9am to 5pm. I know a doctor is available between 9am and 5pm. The patient is looking for doctors that are available from 1pm to 3pm. When he inputs that time range, he should be able to see the doctor in the list. For example, if the doctor is available from 9am to 12pm, and the patient input 3pm to 5pm, the doctor won’t show up. I’m trying to match doctors with appointments with patients where patients give a time range when they are available and so do doctors. How can I do this using javascript? Here’s my inefficient solution.
// startDate and endDate is the time range for the doctor.
const startDateTime = startDate.format('HH:mm');
const endDateTime = endDate.format('HH:mm');
const stimeParts = startDateTime.split(":");
const stime1 = parseInt(stimeParts[0], 10);
const stime2 = parseInt(stimeParts[1], 10);
const etimeParts = endDateTime.split(":");
const etime1 = parseInt(etimeParts[0], 10);
const etime2 = parseInt(etimeParts[1], 10);
const startminutes = (stime1 * 60) + stime2;
const endminutes = (etime1 * 60) + etime2;
// so patients can select times in a 3 hour range
// for example from 0:00 to 3:00 or 12:00 to 15:00
// key is the first number in the time range that the user selected
// so if they select the time range 15:00 to 18:00, then key = 15
const timeSelected = key;
const fromMinutesSelected = timeSelected * 60;
const toMinutesSelected = (timeSelected * 60) + (3 * 60);
let timeStart = startminutes;
while ( found === false && (timeStart < endminutes && ((timeStart + 30) <= endminutes))) {
if (timeStart >= fromMinutesSelected && (timeStart + 30 < toMinutesSelected)) {
found = true;
} else {
timeStart += 30;
}
}
if found == true then the two time ranges overlap. selectedTime