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

202
Vistas
JS Date Object calculate if sendout time is in less than one hour

I have an issue where I am not sure how to calculate if it is one hour or less between a dispatch date and the current time using the JS Date object. what I have done so far is I converted the entity.dispatchDate string into a Date object, however this case that I have written so far returns true whenever todaysDate date object is equal to or greater than the dispatch date. What I think I have to do is first of all, not use 1 hour but instead use minutes, as I need to account for every scenario that is within that hour - e.g. if its one hour before, 15 minutes, or 30 minutes to return true. What I have written so far:

// OPTIONS
const hoursBeforeSendout = 1;
// END OF OPTIONS

if(!entity.dispatchDate) {
    return false;
}
var sendDate = new Date(Date.parse(entity.dispatchDate));

Date.prototype.addHours = function(h) {
  this.setTime(this.getTime() + (h*60*60*1000));
  return this;
}

var todaysDate = new Date();
todaysDate.addHours(hoursBeforeSendout);

if(todaysDate >= sendDate) {
    return true;
}

return false;

I would appreciate any input here on what the best approach would be

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

0

I'd suggest simply subtracting the current date (using Date.now()) from the dispatch date.

If this is under the threshold duration, it's 'due' or 'close':

function isDispatchDateClose(dispatchDate, thresholdMs = 3600 * 1000) {
    if (!dispatchDate) {
        return false;
    }
    const timeToDispatchDateMilliseconds = Date.parse(dispatchDate) - Date.now();
    return (timeToDispatchDateMilliseconds <= thresholdMs);
}

let dispatchDates = [0, 30, 45, 90, 120].map(offsetMinutes => new Date(Date.now() + offsetMinutes * 60000).toLocaleString('sv'));
console.log('Dispatch Date', '\t\t', 'Is Close ( < 1 hr to go)');
for(let dispatchDate of dispatchDates) {
    console.log(dispatchDate, '\t', isDispatchDateClose(dispatchDate))
}
        
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

If all you are wanting is to know if the dispatchDate value is within (less than) 1 hour from now, there are a few ways to do this that are fairly simple.

The simplest method would probably be (updated based on comment by RobG:

return new Date() - sendDate.getTime() >= 3600000;

Depending on whether or not you need any additional flexibility, you could also get the time remaining in a few different forms and decide what to do from there:

// OPTIONS
const hoursBeforeSendout = 1;
// END OF OPTIONS

if(!entity.dispatchDate) {
    return false;
}
var sendDate = new Date(Date.parse(entity.dispatchDate));

let _TimeRemaining = function(d) {
  let timeRemaining = Math.floor((d.getTime() - new Date().getTime()) / 1000);
  return {"hours": Math.floor(timeRemaining / 60 / 60), "minutes": Math.floor(timeRemaining / 60), "seconds": timeRemaining}
}

console.log(_TimeRemaining(sendDate)["hours"]); // returns the hours remaining
console.log(_TimeRemaining(sendDate)["minutes"]); // returns the minutes remaining
console.log(_TimeRemaining(sendDate)["seconds"]); // returns the seconds remaining

return _TimeRemaining(sendDate)["hours"] <= 0; // returns true if less than 1 hour

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