I use this jQuery function in the functions.php file to calculate and display a date, which will be in a certain number of days, starting today, but excluding weekends and holidays.
jQuery(function($) {
if ($('body.page-id-5749').length > 0) {
var monthNames = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
var dayNames = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"];
var holidays = ["2019, 12, 8", // 8 DICEMBRE 2019
"2019, 12, 25", // 25 DICEMBRE 2019
"2019, 12, 26", // 26 DICEMBRE 2019
"2020, 1, 1", // CAPODANNO 2020
"2020, 1, 6", // EPIFANIA 2020
"2020, 4, 13", // PASQUETTA 2020
"2020, 4, 25", // 25 APRILE 2020
"2020, 5, 1", // 1 MAGGIO 2020
"2020, 6, 2", // 2 GIUGNO 2020
"2020, 8, 15", // 15 AGOSTO 2020
"2020, 11, 1", // 1 NOVEMBRE 2020
"2020, 12, 8", // 8 DICEMBRE 2020
"2020, 12, 25", // 25 DICEMBRE 2020
"2020, 12, 26", // 26 DICEMBRE 2020
"2021, 1, 1"
]; // 1 GENNAIO 2021
var endDate = "",
noOfDaysToAdd = $(".valMixBeatVoce").html(),
count = 0;
var someDate = new Date(new Date().toDateString());
var numberOfDaysToAdd = noOfDaysToAdd;
someDate.setDate(someDate.getDate());
while (count < noOfDaysToAdd) {
endDate = new Date(someDate.setDate(someDate.getDate() + 1));
var isHoliday = holidays.find(holiday => endDate.getTime() == new Date(holiday).getTime());
if (isHoliday) {
console.log('holiday, skipping');
} else if (endDate.getDay() != 0 && endDate.getDay() != 6) {
count++;
}
}
$('#DateMixBeatVoce').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);
$('#DateMixBeatVoce2').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);
}
});
But, while on Chrome (desktop) and Firefox (desktop) it works perfectly, on Safari (desktop) and on all mobile browsers (Chrome, Firefox, Safari), the date is calculated excluding weekends, but not excluding holidays. I do not understand why.
Here are the desktop consoles:
Chrome (working):
holiday, skipping (index):880
[Violation] Forced reflow while executing JavaScript took 40ms
(Warning) DevTools failed to parse SourceMap: https://stats.wp.com/s.js.map
Firefox (working):
(Warning) This page uses the non standard property “zoom”. Consider using calc() in the relevant property values, or using “transform” along with “transform-origin: 0 0”. example.com
holiday, skipping example.com:880:17
(Warning) This site appears to use a scroll-linked positioning effect. This may not work well with asynchronous panning example.com
Safari (holidays are not skipped):
[Info] Successfuly preconnected to https://fonts.gstatic.com/
[Error] Failed to load resource: the server responded with a status of 404 () (s.js.map, line 0)
Here is the Reproducible Example: as you can see, it only works on Chrome (desktop) and Firefox (desktop), while holidays are not calculated on Safari (desktop) and on all mobile browsers (Chrome, Firefox, Safari), resulting in a different date shown: