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

230
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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