-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
  • 12,402
  • 10
  • 54
  • 88
user1186050
  • 12,296
  • 22
  • 123
  • 274
  • 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
  • 79,093
  • 40
  • 195
  • 263