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 */

4
  • 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 with a custom callback. Commented May 23, 2018 at 12:21
  • this is because the elapsed ms between the 2 created dates ("2018-05-23") Commented May 23, 2018 at 12:27
  • @Sergi No, that is not the reason. new Date(0) == new Date(0) is also false. Commented May 23, 2018 at 12:37
  • If you check their times (ms) are not the same Commented May 23, 2018 at 13:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.