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
To get the hours and minutes from a date, call the getHours() and getMinutes() methods on the date object. The getHours method returns the hours and the getMinutes() method returns the minutes in the specified date. Copied!
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With