It is important to check for holidays in the code.
The code does not mention checking for holidays explicitly. In my area, "02.01.2017" is considered a public holiday as it falls after New Year's Day on a Sunday. However, "03.01.2017" is not recognized as a holiday.
One way to approach this is by creating an array of specific holiday dates and checking if the given date matches any of them, like so:
// Function to format dates in local ISO 8601 format
if (!Date.prototype.toISODateLocal) {
Date.prototype.toISODateLocal = function() {
return this.getFullYear() + '-' +
('0' + (this.getMonth()+1)).slice(-2) + '-' +
('0' + this.getDate()).slice(-2);
};
}
/* Check if a date is a holiday
** @param {Date} date - date to be checked
** @returns {Boolean} true if it is a holiday,
** otherwise false
*/
function isHoliday(date) {
var dateString = date.toISODateLocal();
var holidays = ['2017-01-02','2017-04-14','2017-04-17'];
return holidays.indexOf(dateString) > -1;
}
[new Date(2017,0,2),
new Date(2017,3,14),
new Date()].forEach(function(date) {
console.log(date.toISODateLocal() + ' is ' +
(isHoliday(date)? '':'not ') + 'a holiday.');
});
Instead of using a range object for contiguous holidays, it is simpler to use an array of individual holiday dates since most holidays are single days. This method works well since there are usually only a few holidays per year.
If you need to determine if a particular date falls between two other dates, you can utilize the comparison operators <
and >
, as demonstrated below:
// Check if date falls within 1 Jan and 10 Jan inclusive
var rangeStart = new Date(2017,0,1); // Start date (01 Jan)
var rangeEnd = new Date(2017,0,10,23,59,59,999); // End date (10 Jan)
[new Date(2017,0,5), // 5 Jan
new Date(2017,0,10), // 10 Jan
new Date(2017,0,11) // 11 Jan
].forEach(function(date) {
console.log(date + ' in range? ' + (date >= rangeStart && date <= rangeEnd));
})