Home > database >  JS Date Object calculate if sendout time is in less than one hour
JS Date Object calculate if sendout time is in less than one hour

Time:01-19

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

CodePudding user response:

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

CodePudding user response:

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

return (new Date().getTime() >= (sendDate.getTime() - 3600000)) ? true : false;

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) true : false; // returns true if less than 1 hour

  •  Tags:  
  • Related