Check if date is less than 1 hour ago?

JavascriptDatetime

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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)                       

Solution 2 - Javascript

Using some ES6 syntax:

const lessThanOneHourAgo = (date) => {
    const HOUR = 1000 * 60 * 60;
    const anHourAgo = Date.now() - HOUR;

    return date > anHourAgo;
}

Using the Moment library:

const lessThanOneHourAgo = (date) => {
    return moment(date).isAfter(moment().subtract(1, 'hours'));
}

Shorthand syntax with Moment:

const lessThanOneHourAgo = (date) => moment(date).isAfter(moment().subtract(1, 'hours'));

Solution 3 - Javascript

the moment library can really help express this. The trick is to take the date, add time, and see if it's before or after now:

  lastSeenAgoLabel: function() {
    var d = this.lastLogin();
    if (! moment(d).isValid()) return 'danger';  // danger if not a date.
    if (moment(d).add(10, 'minutes').isBefore(/*now*/)) return 'danger'; // danger if older than 10 mins
    if (moment(d).add(5,  'minutes').isBefore(/*now*/)) return 'warning'; // warning if older than 5mins
    return 'success';  // Looks good!
  },

Solution 4 - Javascript

Using moment will be much easier in this case, You could try this:

    let hours = moment().diff(moment(yourDateString), 'hours');

It will give you integer value like 1,2,5,0etc so you can easily use condition check like:

if(hours < 1) {

Also, one more thing is you can get more accurate result of the time difference (in decimals like 1.2,1.5,0.7etc) to get this kind of result use this syntax:

let hours = moment().diff(moment(yourDateString), 'hours', true);

Let me know if you have any further query

Solution 5 - Javascript

//for adding hours to a date
Date.prototype.addHours= function(hrs){
    this.setHours(this.getHours()+hrs);
    return this;
}

Call function like this:

//test alert(new Date().addHours(4));

Solution 6 - Javascript

You can do it as follows:

  1. First find difference of two dates i-e in milliseconds
  2. Convert milliseconds into minutes
  3. If minutes are less than 60, then it means date is within hour else not within hour.
var date = new Date("2020-07-12 11:30:10");
var now = new Date();
var diffInMS = now - date;
var msInHour = Math.floor(diffInMS/1000/60);
if (msInHour < 60) {
	console.log('Within hour');
} else {
	console.log('Not within the hour');
}

Solution 7 - Javascript

//try this: // to compare two date's:

<Script Language=Javascript>
function CompareDates() 
{ 
    var str1 = document.getElementById("Fromdate").value;
    var str2 = document.getElementById("Todate").value;
    var dt1  = parseInt(str1.substring(0,2),10); 
    var mon1 = parseInt(str1.substring(3,5),10);
    var yr1  = parseInt(str1.substring(6,10),10); 
    var dt2  = parseInt(str2.substring(0,2),10); 
    var mon2 = parseInt(str2.substring(3,5),10); 
    var yr2  = parseInt(str2.substring(6,10),10); 
    var date1 = new Date(yr1, mon1, dt1); 
    var date2 = new Date(yr2, mon2, dt2); 

    if(date2 < date1)
    {
        alert("To date cannot be greater than from date");
        return false; 
    } 
    else 
    { 
        alert("Submitting ...");
    } 
} 

</Script>

Hope it will work 4 u...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsupercoolvilleView Question on Stackoverflow
Solution 1 - JavascriptMike SamuelView Answer on Stackoverflow
Solution 2 - JavascriptGorka HernandezView Answer on Stackoverflow
Solution 3 - JavascriptMichael ColeView Answer on Stackoverflow
Solution 4 - JavascriptUmang LoriyaView Answer on Stackoverflow
Solution 5 - JavascriptKrishna NView Answer on Stackoverflow
Solution 6 - JavascriptTanzeem BhattiView Answer on Stackoverflow
Solution 7 - JavascriptKrishna NView Answer on Stackoverflow