Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
How to find whether time ranges overlap?

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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here I have a start and end time from 10am to 8pm.
And check every hour if the appointment can be made.

const startDate = new Date('2022-01-24T10:00');
const endDate = new Date('2022-01-24T20:00');

const visitLength = 3 * 60 * 60 * 1000; // 3 hours

const isTimeOk = (key) => {
  const visitStart = new Date(`2022-01-24T${key}:00`);  
  return visitStart >= startDate && endDate - visitStart >= visitLength;
}

// key is hours of visit start
for (let key = 0; key <= 23; key++) {
  console.log(key, isTimeOk(key));
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda