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

229
Views
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 answers
Answer question

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 Report

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