Tengo un trabajo cron ejecutándose en el servidor que realiza alguna acción cada 6 horas UTC.
En la página del cliente, quiero mostrar una cuenta regresiva que muestre el tiempo restante para que se ejecute el próximo trabajo cron.
Si estuviera funcionando a medianoche, podría haberlo hecho.
function timeToMidnight() { var now = new Date(); var end = moment().endOf("day"); return end - now + 1000; }
Pero no puedo averiguar cómo hacer esto durante 6 horas UTC (1200 AM, 0600 AM, 1200 PM, 0600 AM)
Puedes calcularlo bastante fácilmente sin momento.
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()}`);
Sólo cuenta atrás para cualquiera de estos
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())