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

后端 未结 7 1593
借酒劲吻你
借酒劲吻你 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 06:10

    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

    0 讨论(0)
提交回复
热议问题