2

I have the following piece of Javascript in that display a future date. Is there a way i can specify a list of days (WET) for it to not land on? So for example, if the date lands on Christmas, have it automatically + 1 (providing it's still a business day and not another holiday)?

Here's what I'm working with:

function addDates(startDate,noOfDaysToAdd){
  var count = 0;
  while(count < noOfDaysToAdd){
    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
    if(endDate.getDay() != 0 && endDate.getDay() != 6){
       //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
       count++;
    }
  }
  return startDate;
}

function convertDate(d) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
}


var today = new Date();
var daysToAdd = 6;
var endDate = addDates(today,daysToAdd);
document.write(convertDate(endDate));
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
head_bigg
  • 241
  • 1
  • 2
  • 8
  • You probably need a list of days for it to not land on, then compare the future date and if it matches, move it forward by a day, compare again, etc. What have you tried? The *addDates* function looks suspect to me, the entire function can be `startDate.setDate(startDate.getDate() + noOfDaysToAdd)`. The comparison of *getDay* is unnecessary and probably wrong. – RobG Aug 16 '17 at 23:34
  • That's pretty much what I'm hoping to get. Are you able to provide some code by any chance? I'm pretty new to Javascript – head_bigg Aug 17 '17 at 10:26

1 Answers1

0

Adding days to a Date is covered in Add days to JavaScript Date.

To avoid weekends (assuming Saturday and Sunday, some places use different days like Thursday and Friday) you can check the result and move forward if the day is 6 (Saturday) or 0 (Sunday). Similarly you can check for holidays using formatted date strings (easier than using Date objects).

A simple algorithm is to add the required days to a date, then if it lands on a weekend or holiday, keep adding days one at a time until it doesn't.

// Utility functions
// Add days to a date
function addDays(date, days) {
  date.setDate(date.getDate() + days);
  return date;
}

// Return true if date is Sat or Sun
function isWeekend(date) {
  return date.getDay() == 0 || date.getDay() == 6;
}

// Return true if date is in holidays
function isHoliday(date) {
  return holidays.indexOf(formatISOLocal(date)) != -1;
}

// Show formatted date
function showDate(date) {
  return date.toLocaleString(undefined, {
    weekday:'long',
    day: 'numeric',
    month: 'long',
    year: 'numeric'
  });
}

// Return date string in YYYY-MM-DD format
function formatISOLocal(d) {
  function z(n){return (n<10? '0':'')+n}
  return d.getFullYear() + '-' + z(d.getMonth()+1) + '-' + z(d.getDate());
}

// Main function
// Special add days to avoid both weekends and holidays
function specialAddDays(date, days) {

  // Add days
  date.setDate(date.getDate() + days);
  
  // While date is a holiday or weekened, keep adding days
  while (isWeekend(date) || isHoliday(date)) {
    addDays(date, 1);
  }
  return date;
}

// Holidays      Tuesday      Wednesday     Eid al-Adha   Christmas
var holidays = ['2017-08-22','2017-08-23', '2017-09-01', '2017-12-25'];

// Examples
var d = new Date(2017,7,18,1,30);
console.log('Start day: ' + showDate(d)); // Friday
specialAddDays(d, 1);
console.log('Day added: ' + showDate(d)); // Monday

// Tue and Wed are (fake) holidays, so should return Thursday
specialAddDays(d, 1);
console.log('Day added: ' + showDate(d));
RobG
  • 142,382
  • 31
  • 172
  • 209