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

197
Views
Compruebe si el rango de tiempo se superpone con otro rango de tiempo

Supongamos que tengo una fecha de inicio y una fecha de finalización de la siguiente manera

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

Quiero verificar si otro rango de tiempo dado se superpone con el rango de tiempo anterior en javascript. Intenté usar el momento de la siguiente manera.

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

Pero esto no produce los resultados correctos. ¿Cuál sería la forma correcta de verificar si un cierto rango de tiempo se superpone con otro rango de tiempo usando javascript?

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

0

Considera esto:

dibujo sencillo

En el caso A y C no se superponen.

Pero, ¿qué es cierto en B que no es cierto en A o C?

Bueno, el comienzo, o el final del rojo, está entre el comienzo y el final del azul. Eso siempre será cierto para períodos de tiempo superpuestos.

Código de ejemplo:

 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 Report

0

Puede usar el complemento twix.js por un momento para manejar los rangos de fechas. En tu código, necesitas hacer algo así. Este es un fragmento de código de muestra que puede modificar según sus necesidades.

 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 Report

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

hay 3 casos diferentes para considerar como superposición

intervalo1: |-----|
rango de tiempo2: |-----|

intervalo1: |-----|
rango de tiempo2: |-----|

rango de tiempo1: |-----------|
rango de tiempo2: |-----|

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!