Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
Calculate is times between times

I need to create a function that excepts 2 parameters arrays of times and return true or false if the second array between one of the times of the first array:

function calculateIsBetween(workTimes, breakTimes) {
 // ... ... ...
}

example 1:

calculateIsBetween(
[
  {entryTime: '12:00', exitTime: '13:00'},
  {entryTime: '14:00', exitTime: '15:00'}
],


[
  {startBreak: '12:05', endBreak: '12:10'},
  {startBreak: '12:25', endBreak: '12:30'}
]
)

function returns true because breaks between at least one of the entry time and exit time.

example 2:

calculateIsBetween(
[
  {entryTime: '12:00', exitTime: '13:00'},
  {entryTime: '14:00', exitTime: '15:00'}
],
   

[
  {startBreak: '16:05', endBreak: '18:10'},
  {startBreak: '12:25', endBreak: '12:30'}
]
)

function returns false because breaks not between at least one of the entry time and exit time.

p.s function also needs to take care about overnight times.

I tried something with moment but it doesn't work well.

May you can help me

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I will attempt to break down my code, hopefully it is written well enough to be self explanatory. First I need to create an array of allowed ranges for breaks. Each item in the array will have a max for the hours, and a max for the minutes. I use .map to create the array of max - min values from the work times argument passed.

My next line returns the boolean return from a .every test method placed on the breakTimes array. The test that every item in the array must pass is a function which first grabs the start and end values for hour and minute. This test returns true if even one set from the allowed ranges allows the break times.

I did not understand what you meant by taking the next day into consideration, there was no data for the day in the examples given. This should be a good stepping off point though.

If you have any questions about the code, please ask 👋

function calculateIsBetween(workTimes, breakTimes) {
  const allowedRanges = workTimes.map(({entryTime, exitTime}) => {
    const [hMin, mMin] = entryTime.split(":");
    const [hMax, mMax] = exitTime.split(":");
    return {hMin, hMax, mMin, mMax};
  });
  return breakTimes.every(({startBreak, endBreak}) => {
    const [hStart, mStart] = startBreak.split(":");
    const [hEnd, mEnd] = endBreak.split(":");
    return allowedRanges.some(({hMin, hMax, mMin, mMax}) => {
      if (
        hStart < hMin ||
        hEnd > hMax
      ) return false;
      if (
        hStart > hMin &&
        hEnd < hMax
      ) return true;
      if (
        (hStart == hMin && mStart < mMin) ||
        (hEnd == hMax && mEnd > mMax)
      ) return false;
      return true;
    });
  });
}
console.log(
  calculateIsBetween(
    [
      {entryTime: '12:00', exitTime: '13:00'},
      {entryTime: '14:00', exitTime: '15:00'}
    ],
    [
      {startBreak: '12:05', endBreak: '12:10'},
      {startBreak: '12:25', endBreak: '12:30'}
    ]
  )
); //returns true
console.log(
  calculateIsBetween(
    [
      {entryTime: '12:00', exitTime: '13:00'},
      {entryTime: '14:00', exitTime: '15:00'}
    ],
    [
      {startBreak: '16:05', endBreak: '18:10'},
      {startBreak: '12:25', endBreak: '12:30'}
    ]
  )
); //returns false
.as-console-wrapper { max-height: 100% !important; top: 0;}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda