Comparing date part only without comparing time in JavaScript

后端 未结 22 1462
春和景丽
春和景丽 2020-11-22 10:59

What is wrong with the code below?

Maybe it would be simpler to just compare date and not time. I am not sure how to do this either, and I searched, but I couldn\'t

22条回答
  •  粉色の甜心
    2020-11-22 11:36

    How about this?

    Date.prototype.withoutTime = function () {
        var d = new Date(this);
        d.setHours(0, 0, 0, 0);
        return d;
    }
    

    It allows you to compare the date part of the date like this without affecting the value of your variable:

    var date1 = new Date(2014,1,1);
    new Date().withoutTime() > date1.withoutTime(); // true
    

提交回复
热议问题