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

后端 未结 7 1613
借酒劲吻你
借酒劲吻你 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: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');
    }
    

提交回复
热议问题