Checking if two Dates have the same date info

后端 未结 8 672
清歌不尽
清歌不尽 2020-11-27 05:40

How can I check if two different date objects have the same date information(having same day, month, year ...)? I have tried "==", "===" and .equals but

8条回答
  •  一向
    一向 (楼主)
    2020-11-27 06:30

    Hellnar,

    you could try (pardon the function name :) - amended per felix's valueof, rather than getTime)

    function isEqual(startDate, endDate) {
        return endDate.valueOf() == startDate.valueOf();
    }
    

    usage:

    if(isEqual(date1, date2)){
       // do something
    }
    

    might get you part of the way there.

    see also:

    'http://www.java2s.com/Tutorial/JavaScript/0240__Date/DatevalueOf.htm'

提交回复
热议问题