-2

If I have two Javascript date objects how can I see if my selectedDate is at least one hour into the future? Ex. if its 3PM, the selectedDate needs to be at least 4PM

var selectedDate = //somedatetime object
var currentDate = new Date(); // includes current time


if (selectedDate is before currentDateTime + 1 hour) {
    // return false
}
Ghilas BELHADJ
  • 13,412
  • 10
  • 59
  • 99
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • Check out moment.js. It will make life much easier – WillardSolutions Mar 23 '16 at 22:41
  • Use the answer I linked to then you can use [`getHours`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours) to see how many hours are in between them. – Mike Cluck Mar 23 '16 at 22:42
  • 1
    Add an hour to the current date and compare the two? You might want to search for the two parts of the task on their own to find an answer. – Bergi Mar 23 '16 at 22:42

1 Answers1

0

The value of dates are measured in milliseconds. So just look if it is 1 hour worth of milliseconds (3600000) behind or not.

if (+selectedDate < +currentDateTime + 3600000) {
    //more than an hour has passed
}
Travis J
  • 81,153
  • 41
  • 202
  • 273