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

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

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 Relatório

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 Relatório

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 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