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

194
Visualizações
Check if time range overlaps with another time range

Assume i have a start date and a end data as follows

end: "2021-10-22T06:00:00.000Z"
start: "2021-10-22T05:00:00.000Z"

I want to check if another given time range overlaps with the above time range in javascript. I tried using moment as follows.

return (
            moment(timerange1.duration.end) <= moment(timerange2.duration.start) ||
            moment(timerange1.duration.start) >= moment(timerange2.duration?.end)
          );

But this does not produce the correct results. What would be the correct way to check if a certain time range overlaps with another time range using javascript?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Consider this:

Simple drawing

In case A and C they do not overlap.

But what is true in B that isn't true in A or C?

Well, the start, or the end of the red one, is between the start and end of the blue one. That will always be true for overlapping timeperiods.

Example code:

if( (red.start > blue.start && red.start < blue.end) || (red.end > blue.start && red.end< blue.end) ) {
  // do something
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the twix.js plugin for moment to handle the date ranges. In your code, you need to do something like that. This is sample code snippet you can modify it according to your need.

var t1 = {
  start: "1982-01-25T09:30",
  end: "1982-01-25T13:30",
};
var t2 = {
  start: "1982-01-23T13:30",
  end: "1982-01-25T12:30",
};

var t3 = {
  start: "1982-01-24T13:30",
  end: "1982-01-25T10:30",
};

var t1Range = moment(t1.start).twix(t1.end);
var t2Range = moment(t2.start).twix(t2.end);
var t3Range = moment(t3.start).twix(t3.end);

t1Range.overlaps(t2Range) || t1Range.overlaps(t3Range); //=> true
about 4 years ago · Juan Pablo Isaza Relatório

0

return (
      (moment(timerange1.duration.start) >= moment(timerange2.duration.start) && moment(timerange1.duration.start) <= moment(timerange2.duration.end))
|| (moment(timerange1.duration.end) >= moment(timerange2.duration.start) && moment(timerange1.duration.end) <= moment(timerange2.duration.end))
|| (moment(timerange2.duration.start) >= moment(timerange1.duration.start) && moment(timerange2.duration.start) <= moment(timerange1.duration.end))
)

there are 3 different cases to consider as overlap

timerange1: |-----|
timerange2:     |-----|

timerange1:     |-----|
timerange2: |-----|

timerange1: |-----------|
timerange2:     |-----|

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