Skip to content
Advertisement

Check if date is less than 1 hour ago?

Is there a way to check if a date is less than 1 hour ago like this?

// old date
var olddate = new Date("February 9, 2012, 12:15");

// current date
var currentdate = new Date();

if (olddate >= currentdate - 1 hour) {
    alert("newer than 1 hour");
else {
    alert("older than 1 hour");
}

Also, different question – is there a way to add hours to a date like this?

var olddate = new Date("February 9, 2012, 12:15") + 15 HOURS; // output: February 10, 2012, 3:15

Advertisement

Answer

Define

var ONE_HOUR = 60 * 60 * 1000; /* ms */

then you can do

((new Date) - myDate) < ONE_HOUR

To get one hour from a date, try

new Date(myDate.getTime() + ONE_HOUR)                       
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement