Moment.js Check a date is today

后端 未结 5 1312
礼貌的吻别
礼貌的吻别 2020-12-25 10:30

How do I check a date is actually today (the same date) rather than the difference between hours in a day?

I have three timestamps as examples, one is today (22/07/1

5条回答
  •  庸人自扰
    2020-12-25 10:55

    You can try this,

    var today = 1406019110000; // today
    var yesterday = 1405951867000; // yesterday
    
    checkToday(today);
    checkToday(yesterday);
    
    function checkToday(timestamp)
    {
        if (moment(timestamp).format('DD/MM/YYYY') == moment(new Date()).format('DD/MM/YYYY'))
            alert('true');
        else
            alert("false");
    }
    

    Here is the demo

提交回复
热议问题