Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  37] [ 3]  / answers: 1 / hits: 128024  / 13 Years ago, fri, february 10, 2012, 12:00:00

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

More From » datetime

 Answers
81

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)                       

[#87557] Wednesday, February 8, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ellisc

Total Points: 533
Total Questions: 82
Total Answers: 90

Location: Bangladesh
Member since Thu, Aug 5, 2021
3 Years ago
;