Reputation: 164
I have a function that checks wether a current time is within working hours or not - currently i only check for working hours, daylight savings, and weekend all with boolean functions.
How can I set up a list of holiday dates and have the function check if the CURRENT day today is an holiday or not and return true if it is or false if it isn't.
Is the best method to do that is create an array of dates, like this?
var holidays = { // keys are formatted as month,day
"0,1": "Martin Luther King, Jr. Day",
"1,1": "President's Day",
"2,0": "Daylight Savings Time Begins",
"3,3": "Administrative Professionals Day",
"4,0": "Mother's Day",
"4,1": "Memorial Day",
"5,0": "Father's Day",
"6,0": "Parents Day",
"8,1": "Labor Day",
"8,0": "Gold Star Mothers Day",
"9,1": "Columbus Day",
"10,0": "Daylight Savings Time Ends",
"10,4": "Thanksgiving Day"
};
//or like this?
var holidays = ["Martin Luther King, Jr. Day": {
"day":"1",
"month":"0"
},
"President's Day": {
"day":"1",
"month":"1"
}];
And then check if date.getMonth() date.getDate()
equals one of the items in the list then return true else false
Would something like this work?
function checkHoliday(){
month = date.getMonth()
date = date.getDate()
for (var i=0; i < holidays.length ;i++){
if(holidays[i].day == date && holidays[i].month == month) {
return true
} else return false;
}
Upvotes: 1
Views: 1954
Reputation: 2590
I would do something like this:
const holidays = [
{name: 'Martin Luther King, Jr. Day', day: 20, month: 1},
{name: 'Presidents Day', day: 17, month: 2}
];
function checkHoliday(){
const today = new Date(); // get date of today
for(var i = 0; i < holidays.length; i++){
let isHoliday = holidays[i].day === today.getDate() && holidays[i].month === today.getMonth() + 1; // check if today is a holiday
if(isHoliday) return true; // return true if today is a holiday
// you could also return which holiday it is using: if(isHoliday) return holidays[i].name;
}
return false; // else return false
}
console.log(checkHoliday());
But, I think oshells idea about this is better than mine. Only one more thing:
You could return the name of the holiday instead of true, if there is no holiday on the day it will return undefined.
const holidays = {
"0,1": "Martin Luther King, Jr. Day",
"1,1": "President's Day",
"2,0": "Daylight Savings Time Begins",
"3,3": "Administrative Professionals Day",
"4,0": "Mother's Day",
"4,1": "Memorial Day",
"5,0": "Father's Day",
"6,0": "Parents Day",
"8,1": "Labor Day",
"8,0": "Gold Star Mothers Day",
"9,1": "Columbus Day",
"10,0": "Daylight Savings Time Ends",
"10,4": "Thanksgiving Day"
};
let date = new Date();
function checkHoliday() {
const month = date.getMonth();
const day = date.getDate();
return holidays[month + ',' + day];
}
// As you can see, this could also be used like a boolean:
if(checkHoliday()) console.log('true because the function returns:', checkHoliday());
else console.log('false because the function returns:', checkHoliday());
// The following is only to fake a holiday:
document.getElementById('makeHoliday').onclick = () => {
date = new Date(2019, 0, 1);
if(checkHoliday()) console.log('true because the function returns:', checkHoliday());
else console.log('false because the function returns:', checkHoliday());
}
<button id="makeHoliday">Klick here to fake a holiday</button>
Upvotes: 1
Reputation: 9123
Example
function checkHoliday(){
var month = date.getMonth();
var date = date.getDate();
return holidays.hasOwnProperty(month + ',' + date);
}
Upvotes: 3