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

154
Views
How to calculate time overlapping in Javascript

I want o find overlapped time slot with small code. Lets there are time slots:

const slots = [
    {
    start_time: '10:30',
    end_time: '11:00'
  },
  {
    start_time: '11:30',
    end_time: '12:00'
  }
]

And I got the value from user as:

const inputStartTime = '10:50';
const inputEndTime = '11:20';

I think, there will be no overlapping if inputStartTime > slots.end_time and inputEndTime < slots.start_time. So, I want to set a variable such as const overlappedSlots = // and get the output of that variable as (console.log(overlappedSlots)):

[{
  start_time: "10:30",
  end_time: "11:00"
}]

But, I can't get this output by writing this function:

const inputStartTime = '10:50';
const inputEndTime = '11:20';

const slots = [
    {
    start_time: '10:30',
    end_time: '11:00'
  },
  {
    start_time: '11:30',
    end_time: '12:00'
  }
]

const overlappedSlots = slots.filter(slot => !slot.end_time < inputStartTime || !slot.start_time > inputEndTime);
console.log(overlappedSlots)

What I was doing wrong there?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The error is when you use ! without parenthesis

const inputStartTime = '10:50';
const inputEndTime = '11:20';

const slots = [{
    start_time: '10:30',
    end_time: '11:00',
  },
  {
    start_time: '11:30',
    end_time: '12:00',
  },
];

const overlappedSlots = slots.filter(
  (slot) => inputStartTime > slot.start_time || !(slot.start_time > inputEndTime));
console.log(overlappedSlots);

about 4 years ago · Juan Pablo Isaza Report

0

the problem with your code remains with ! Try this :

const overlappedSlots = slots.filter(slot => !(slot.end_time < inputStartTime) || !(slot.start_time > inputEndTime));

    console.log("over"+overlappedSlots);
about 4 years ago · Juan Pablo Isaza Report

0

use && instead of || this will give you the result you want
time start : 10:30 end time: 11:00

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!