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

175
Vistas
Find consecutive login timings from javascript array list

I have an array of objects in javascript where I have employees login timings. I want to find if an employee has worked more then 5 hours consecutively then he/she will get a break. Otherwise if an employee worked more than 5 hours, but not consecutively, then they do not get a break.

Here is the code - it currently is just checking the number of hours an employee has worked, but it is not checking for consecutive hours.

checkBreakTimings() {

    let breakTimes = "";
    

    const timings = [
        {
       timeIn: '11:00',
       timeOut: '12:00',        
        },
        {
       timeIn: '12:00',
       timeOut: '13:00',        
        },
        {
       timeIn: '14:00',
       timeOut: '16:00',        
        },
        {
       timeIn: '16:00',
       timeOut: '18:00',        
        }
    ];

   let h = 0;
   
      timings.forEach(e => {
        const ms = moment(e.timeOut, "HH:mm").diff(moment(e.timeIn, "HH:mm"));
        h = h + = Number(moment.duration(ms).asHours());
      });
  
      if(h > 5 )
      {
        breakTimes = "Yes";
      }
    }
    
    return breakTimes;
  }

As you can see from the above timings that an employee worked 8 hours out for which 6 hours are consecutive from 14:00 to 18:00 here break will be applied, but the above 2 timings are not consecutively more than 5 hours.

My problem is - is there any way to find out the number of consecutive hours from an array using JavaScript or in moment.js

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

0

Try like following snippet:

const checkBreakTimings = () => {
  let breakTimes = "";
  const timings = [
      {timeIn: '11:15', timeOut: '12:00',  },
      {timeIn: '12:00', timeOut: '13:00',  },
      {timeIn: '13:00', timeOut: '15:46',  },
      {timeIn: '16:00', timeOut: '18:00',  }
  ];
  let h = 0;
  let consecutive = 0
  let lastOut = null
  timings.forEach(e => {
    const ms = moment(e.timeOut, "HH:mm").diff(moment(e.timeIn, "HH:mm"));
    let worked = Number(moment.duration(ms).asHours())
    consecutive = worked  
    if (lastOut === e.timeIn) {  //👈 compare if timeIn is equal to last timeOut and sum values
      consecutive += worked
    } else consecutive = 0  
    lastOut = e.timeOut
    if (consecutive > 5) breakTimes = "Yes" //👈 it's more then 5 hours
    h = h + Number(moment.duration(ms).asHours());
  });
  console.log(breakTimes);
}
checkBreakTimings()
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is a function that could do the trick

function isAllowedBrake(times) {
  let isAllowed = false
  let consecutive = 0;
  let lastVal

  times.forEach((e) => {
    if (lastVal && moment(lastVal, "HH:mm").isBefore(moment(e.timeIn, "HH:mm"))) {
      consecutive = 0
    }
    const ms = moment(e.timeOut, "HH:mm").diff(moment(e.timeIn, "HH:mm"));
    let duration = Number(moment.duration(ms).asHours())
    consecutive += duration
    lastVal = e.timeOut
  })

  if (consecutive > 5) {
    isAllowed = true;
  }

  return isAllowed;
}

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