0

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>

Codepen with current code

Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
  • 1
    This should help: http://stackoverflow.com/questions/28425132/how-to-calculate-number-of-working-days-between-two-dates-in-javascript-using – Rajesh Sep 07 '16 at 15:47
  • Don't write your own date arithmetic code. There are libraries for that, most prominently [moment.js](http://momentjs.com/), which has everything you could wish for, including a plugin interface (random finds are https://gist.github.com/jrhames/5200024 and https://www.npmjs.com/package/moment-business-days). Your time is better spent learning a well-tested library. – Tomalak Sep 07 '16 at 15:56
  • You have `if (noWeekendsOrHolidays(date))`, but *noWeekendsOrHolidays* may return the value returned by *nationalDays(date)*, which is an array so always evaluates to true. Perhaps it should return `nationalDays(date)[0]`. – RobG Sep 08 '16 at 06:49

0 Answers0