Is there a way to check if a date is less than 1 hour ago like this?
JavaScript
x
12
12
1
// old date
2
var olddate = new Date("February 9, 2012, 12:15");
3
4
// current date
5
var currentdate = new Date();
6
7
if (olddate >= currentdate - 1 hour) {
8
alert("newer than 1 hour");
9
else {
10
alert("older than 1 hour");
11
}
12
Also, different question – is there a way to add hours to a date like this?
JavaScript
1
2
1
var olddate = new Date("February 9, 2012, 12:15") + 15 HOURS; // output: February 10, 2012, 3:15
2
Advertisement
Answer
Define
JavaScript
1
2
1
var ONE_HOUR = 60 * 60 * 1000; /* ms */
2
then you can do
JavaScript
1
2
1
((new Date) - myDate) < ONE_HOUR
2
To get one hour from a date, try
JavaScript
1
2
1
new Date(myDate.getTime() + ONE_HOUR)
2