Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!