Checking if two Dates have the same date info Checking if two Dates have the same date info javascript javascript

Checking if two Dates have the same date info


You can use valueOf() or getTime():

a = new Date(1995,11,17);b = new Date(1995,11,17);a.getTime() === b.getTime() // prints true


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}


I used this code:

Date.prototype.isSameDateAs = function(pDate) {  return (    this.getFullYear() === pDate.getFullYear() &&    this.getMonth() === pDate.getMonth() &&    this.getDate() === pDate.getDate()  );}

Then you just call it like : if (aDate.isSameDateAs(otherDate)) { ... }