0

Having problems with this:

var arr = new Array();
var d = new Date("2018-05-23");

arr.push(new Date("2017-10-01"))
arr.push(new Date("2018-05-23"))
arr.push(new Date("2018-12-31"))
var y = arr.includes(d);
console.log("Y: ", y);
var y = arr.indexOf(d);
console.log("Y: ", y);
/* Returns false and -1 */
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 3
    Objects are only ever equal if they're the same object. Date objects don't compare as equal whether they represent the same date or not. You would have to `reduce` the array or use `find`/[`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) with a custom callback. – deceze May 23 '18 at 12:21
  • this is because the elapsed ms between the 2 created dates ("2018-05-23") – Sergi Dote Teixidor May 23 '18 at 12:27
  • @Sergi No, that is not the reason. `new Date(0) == new Date(0)` is also `false`. – deceze May 23 '18 at 12:37
  • If you check their times (ms) are not the same – Sergi Dote Teixidor May 23 '18 at 13:09

0 Answers0