Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda