Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 523
supercoolville Avatar asked Feb 10 '12 08:02

supercoolville


People also ask

How do you find the hour and minute of a date object?

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!


1 Answers

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)                        
like image 95
Mike Samuel Avatar answered Oct 19 '22 13:10

Mike Samuel