0

Maybe I missing something obvious but I don't know what I'm doing wrong. Im trying to write a function which returns true if a given date is a UK holiday.

Ive created a small array to test my function:

const holidays = [
new Date(2016, 0, 1),  //NewYearsDay
new Date(2016, 2, 25),  //GoodFriday
new Date(2016, 2, 27),  //Easter
];

Then my function is

function isHoliday(date){
    return holidays.some(function(d) {
        return (d == date);
    });
}

To test it I ran

 date = new Date(2016, 0, 1);
 var a = isHoliday(date);
 alert(a); 

But the alert says false ??

Connor Bishop
  • 921
  • 1
  • 12
  • 21

1 Answers1

1

You cannot test two Date objects for equality.
Just test the date's timestamps which is a numeric value

function isHoliday(date){
    return holidays.some(function(d) {
        return (d.getTime() == date.getTime());
    });
}
eltonkamami
  • 5,134
  • 1
  • 22
  • 30