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

231
Vistas
Counter that resets every 6 hours UTC using moment js

I have a cron job running on the server that performs some action every 6 hours UTC.

On the client page, I want to show a countdown that shows the time remaining for the next cron job to run.

If it was running at midnight I could have done

function timeToMidnight() {
   var now = new Date();
   var end = moment().endOf("day");

   return end - now + 1000;
}

But I am not able to figure out how to do this for 6 hourly UTC (1200 AM, 0600 AM, 1200 PM, 0600 AM)

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

0

You can calculate it quite easily without moment

const timeToNextRun = (start) => {
  const sixHoursInMs = 6 * 3600 * 1000;
  let remainingTime = sixHoursInMs - (start.getTime() % sixHoursInMs);
  return remainingTime;
};

let now = new Date();
let countdown = timeToNextRun(now);

console.log(`Setting timer for ${countdown}ms - ${new Date(now.getTime() + countdown).toISOString()}`);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just count down to any of these

const getDates = () => {
  const d = new Date()
  const utcDate = new Date(Date.UTC(d.getFullYear(),d.getMonth(), d.getDate(),6,0,0))
  return [utcDate,new Date(utcDate.getTime() + (21600000)),new Date(utcDate.getTime() + (21600000*2)),new Date(utcDate.getTime() + (21600000*3))]
}
console.log(getDates())

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