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 none seems to work.
9 Answers
You can use valueOf()
or getTime()
:
a = new Date(1995,11,17);
b = new Date(1995,11,17);
a.getTime() === b.getTime() // prints true

- 10,244
- 7
- 28
- 56

- 795,719
- 175
- 1,089
- 1,143
-
21Because in case of `>=` the values are converted to numbers. In case of `==` the objects are compared themselves (and only `a == a` would be true). – Felix Kling Jun 07 '13 at 22:10
-
12Just pointing out that this solution requires that the dates only consist of the date parts, while a lot of people coming to this question might have different requirements ([example with time parts in jsFiddle](https://jsfiddle.net/KyleMit/ez6sjn34/)) – KyleMit Apr 10 '17 at 20:10
-
2So when using this be sure time parts are set to 0 thanks to the the [setHours function](https://www.w3schools.com/jsref/jsref_sethours.asp) : `a.setHours(0,0,0,0);` – COil Aug 07 '18 at 12:17
-
4This is not going to work if there is any difference of milliseconds. Since `getTime()` returns the time in milliseconds, both of the times can have difference of milliseconds as well. In this solution, it will not work as expected. Try using `toDateString()` – Rehan Sattar Nov 14 '19 at 13:10
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
}

- 38,105
- 35
- 175
- 251
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)) { ... }

- 4,249
- 3
- 23
- 30
-
Why would you run a dozen functions when you can use getTime() just twice? Oh, were you thinking about ignoring differences in hour, month, and year? In that case maybe time zone considerations start to matter? – ErikE May 02 '11 at 23:15
-
15Because the question was "two different date objects have the same date information", and this code does just that: compares dates regardless of time – Incidently May 05 '11 at 18:37
-
2
-
Based on the question as precisely asked, this is in fact the correct answer, all be it, "date info" can be taken as ambiguous. – Eddie Jul 12 '21 at 21:17
Type convert to integers:
a = new Date(1995,11,17);
b = new Date(1995,11,17);
+a === +b; //true

- 2,014
- 1
- 22
- 25
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'

- 22,305
- 4
- 49
- 63
subtract them and compare to zero:
var date1 = new Date();
var date2 = new Date();
// do something with the dates...
(date1 - date2) ? alert("not equal") : alert("equal");
to put it into a variable:
var datesAreSame = !(date1 - date2);

- 13,971
- 2
- 30
- 51
A simple single line alternative for determining if two dates are equal, ignoring the time part:
function isSameDate(a, b) {
return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}
It determines if dates a and b differ no more than one day and share the same day of the week.
function isSameDate(a, b) {
return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}
console.log(isSameDate(new Date(2017, 7, 21), new Date(2017, 7, 21))); //exact same date => true
console.log(isSameDate(new Date(2017, 7, 21, 23, 59, 59), new Date(2017, 7, 21))); //furthest same dates => true
console.log(isSameDate(new Date(2017, 7, 20, 23, 59, 59), new Date(2017, 7, 21))); //nearest different dates => false
console.log(isSameDate(new Date(2016, 7, 21), new Date(2017, 7, 21))); //different year => false
console.log(isSameDate(new Date(2017, 8, 21), new Date(2017, 7, 21))); //different month => false

- 4,955
- 2
- 36
- 44
For better date support use moment.js and isSame method
var starDate = moment('2018-03-06').startOf('day');
var endDate = moment('2018-04-06').startOf('day');
console.log(starDate.isSame(endDate)); // false ( month is different )
var starDate = moment('2018-03-06').startOf('day');
var endDate = moment('2018-03-06').startOf('day');
console.log(starDate.isSame(endDate)); // true ( year, month and day are the same )

- 2,227
- 4
- 41
- 58
i like to use this code
function isEqualDate(d1, d2) {
return (
d1.toISOString().startsWith(d2.toISOString().substring(0,10))
);
}
console.log(isEqualDate(new Date(2022,2,2,10,57), new Date(2022,2,2,11,57)) )
console.log(isEqualDate(new Date(), new Date(2022,2,2)) )
console.log(isEqualDate(new Date(), new Date()) )

- 1,711
- 1
- 13
- 16