I am trying to get this script to display 3 dates in the future, estimating the arrival time if the customer orders today. In advertising and on the homepage I want to show 4, 10 and 12 business days in the future, formatted as "Monday, January 12th", to allow customers to know approximately when they might get their labels.
I am able to get three dates in the future, but not accounting for the weekends or any of the dates in the 'holiday date array,'
I just need to count 4, 10, and 12 business days into the future., but the code shown below is counting days without skipping weekends or holidays.
What am I doing wrong?
My current JS / jQuery code:
var natDays = [
[1, 1, 'New Year'],
[1, 20, 'Martin Luther King'],
[2, 17, 'Washingtons Birthday'],
[5, 26, 'Memorial Day'],
[7, 4, 'Independence Day'],
[9, 12, 'Labour Day'],
[10, 14, 'Columbus Day'],
[11, 11, 'Veterans Day'],
[11, 28, 'Thanks Giving Day'],
[12, 25, 'Christmas']
];
// dateMin is the minimum delivery date
var dateMin = new Date();
dateMin.setDate(dateMin.getDate() + (dateMin.getHours() >= 14 ? 1 : 0));
function AddBusinessDays(curdate, weekDaysToAdd) {
var date = new Date(curdate.getTime());
while (weekDaysToAdd > 0) {
date.setDate(date.getDate() + 1);
//check if current day is business day
if (noWeekendsOrHolidays(date)) {
weekDaysToAdd--;
}
}
return date;
}
function noWeekendsOrHolidays(date) {
var noWeekend = jQuery.datepicker.noWeekends(date);
return (noWeekend[0] ? nationalDays(date) : noWeekend);
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
function setDeliveryDate(date) {
jQuery('#normal-date').text(jQuery.datepicker.formatDate('DD, MM dd', date));
}
function setRushDeliveryDate(date) {
jQuery('#rush-date').text(jQuery.datepicker.formatDate('DD, MM dd', date));
}function setSuperDeliveryDate(date) {
jQuery('#superrush-date').text(jQuery.datepicker.formatDate('DD, MM dd', date));
}
setDeliveryDate(AddBusinessDays(dateMin, 12));
setRushDeliveryDate(AddBusinessDays(dateMin, 10));
setSuperDeliveryDate(AddBusinessDays(dateMin, 4));
;
HTML:
<p class="delvdate">Guaranteed by <span id="normal-date"></span>*</p>
<p class="delvdate">Guaranteed by <span id="rush-date"></span>*</p>
<p class="delvdate">Guaranteed by <span id="superrush-date"></span>*</p>