JS: Check if date is less than 1 hour ago?

后端 未结 7 1592
借酒劲吻你
借酒劲吻你 2020-12-13 05:38

Is there a way to check if a date is less than 1 hour ago?

Something like this:



        
相关标签:
7条回答
  • 2020-12-13 05:48

    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)                       
    
    0 讨论(0)
  • 2020-12-13 05:48
    //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));
    
    0 讨论(0)
  • 2020-12-13 05:51

    //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...

    0 讨论(0)
  • 2020-12-13 05:54

    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');
    }
    
    0 讨论(0)
  • 2020-12-13 05:55

    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!
      },
    
    0 讨论(0)
  • 2020-12-13 06:02

    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'));
    
    0 讨论(0)
提交回复
热议问题