Comparing date part only without comparing time in JavaScript

后端 未结 22 1480
春和景丽
春和景丽 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:54

    Just use toDateString() on both dates. toDateString doesn't include the time, so for 2 times on the same date, the values will be equal, as demonstrated below.

    var d1 = new Date(2019,01,01,1,20)
    var d2 = new Date(2019,01,01,2,20)
    console.log(d1==d2) // false
    console.log(d1.toDateString() == d2.toDateString()) // true
    

    Obviously some of the timezone concerns expressed elsewhere on this question are valid, but in many scenarios, those are not relevant.

提交回复
热议问题