Checking if two Dates have the same date info

后端 未结 8 667
清歌不尽
清歌不尽 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:12

    If you are only interested in checking if dates occur on the same day regardless of time then you can use the toDateString() method to compare. This method returns only the date without time:

    var start = new Date('2015-01-28T10:00:00Z');
    var end = new Date('2015-01-28T18:00:00Z');
    
    if (start.toDateString() === end.toDateString()) {
      // Same day - maybe different times
    } else {
      // Different day
    }
    

提交回复
热议问题