How can I check Holidays from array


#1

Hi All,

I need help to validate bank holiday from the array if it is similar to entered full date.
This is my code pen link CodePen link

Thank you


#2

Could you describe the problem you’re having, and what you expect this to do?


#3

I want to check both dates (input) bank holidays or not… if it is a bank holiday I want to show error message using array @PortableStick


#4

You have a function called checkHolidays right in the middle of your validateEventDate function (which is not a great location), but you never call it. Where do you want to call this function in the code? Also, you need to review the documentation on the every function, because you are not going to achieve what you want with they way you are trying to use it right now.

Hopefully, this will help you focus on the part you need to correct.


#5

I am struggling to achieve

  1. convert string array to date array
  2. then loop through all array value until it matches 1 or more ‘eventDate’ ,
    Thank you

#6

@rmdawson71, @PortableStick
I fixed the main issue , but somehow array isn’t looping through for check all the value. Any fix for this?
var holidays = [new Date(“2017/12/25”), new Date(“2017/12/26”)];
for (var i = 0; i < holidays.length; i++) {
if (eventDate.getTime() !== holidays[i].getTime()) {
return true
} else {
return false
}
}


#7

@ShenikaFernando - I looked at your actual code on the code pen site. If you modified the for loop section (below) which is currently commented:

                    for (var i = 0; i < holidays.length; i++) {
                       if (eventDate.getTime() !== holidays[i].getTime()) {
                         console.log('foo');
                         return true
                       } else {
                         return false
                       }
                    }

to be:

                    for (var i = 0; i < holidays.length; i++) {
                       if (eventDate.getTime() !== holidays[i].getTime()) {
                         return false;
                       }
                    }

it should work as you want it to. You also need to get rid of the code section below which is located above the commented for loop.

                    var eventHoliday = holidays.filter(function(holiday) {
                     console.log(holiday === eventDate) 
                     return holiday === eventDate
                    });

Lastly, my suggestion is to create a stand-alone function called isHoliday which takes one argument called theDate and returns true if theDate is one of the holidays. I would put the holidays array inside the function, so you know right where to modify the holidays array in the future if needed. Then any time you want to check if a date is one of the holidays, then you would simply call it. By creating a stand-alone function, if you also wnat to check your event end date, you are not repeating code to accomplish the same thing. See below for function and how you would call it in your current code:

function isHoliday(theDate) {
  var holidays = [new Date("2017/12/25"), new Date("2017/12/26"), new Date("2017/12/27")];
  for (var i = 0; i < holidays.length; i++) {
    if (theDate.getTime() === holidays[i].getTime()) {
      return true;
    }
  }
  return false;
}
var eventDate = new Date("2017/12/26")
if (isHoliday(eventDate)) {
  console.log('date is a holiday');
  // in your current code you would simply return false  here
}

or if you want to use a higher level function and take advantage of ES6 arrow functions, then your isHoliday function could look like this:

function isHoliday(theDate) {
  var holidays = [new Date("2017/12/25"), new Date("2017/12/26"), new Date("2017/12/27")];
  return holidays.some(holi => holi.getTime() === theDate.getTime());
}

#8

Thank you @rmdawson71 for your time and depth explanation. Finally, I understood what I made wrong and improvements to going ahead.