Skip to content Skip to sidebar Skip to footer

Set Date To 7 Working Days From Today (excluding Weekends And Public Holidays)

I'm trying to set a date to 7 working days from today's date (excluding weekends and UK public holidays). I start by setting the default date to today's date (todaysDate) + 7 days

Solution 1:

Check one day at a time instead of a 7 day range.

Start by setting the default date to today's date. Then, check one day at a time into the future. If that day is a working day, increment the workingDay counter by 1. if it's not, just loop onto the following day. When your workingDay counter hits 7, that's the date you need.

Solution 2:

Here is an example of what @andi is talking about. I made it as a calculator object.

var calculator = {
    workDaysAdded: 0,
    ukHolidays: ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26'],
    startDate: null,
    curDate: null,

    addWorkDay: function() {
        this.curDate.setDate(this.curDate.getDate() + 1);
        if(this.ukHolidays.indexOf(this.formatDate(this.curDate)) === -1 && this.curDate.getDay() !== 0 && this.curDate.getDay() !== 6) {
            this.workDaysAdded++;
        }
    },

    formatDate: function(date) {
        var day = date.getDate(),
            month = date.getMonth() + 1;

        month = month > 9 ? month : '0' + month;
        day = day > 9 ? day : '0' + day;
        return date.getFullYear() + '-' + month + '-' + day;
    },

    getNewWorkDay: function(daysToAdd) {
        this.startDate = newDate();
        this.curDate = newDate();
        this.workDaysAdded = 0;
        
        while(this.workDaysAdded < daysToAdd) {
            this.addWorkDay();
        }
        returnthis.curDate;
    }
}

var newWorkDay7 = calculator.getNewWorkDay(7);
var newWorkDay9 = calculator.getNewWorkDay(9);
var newWorkDay14 = calculator.getNewWorkDay(14);
console.log(newWorkDay7);
console.log(newWorkDay9);
console.log(newWorkDay14);

Solution 3:

If you only want to add 7 days and exclude holidays and weekends (assuming Saturday and Sunday), then you can just step from the start to the end and test each day as you go:

var ukHolidays = ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26'];

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

// Modifies date by adding 7 days, excluding sat, sun and UK holidaysfunctionadd7WorkingDays(date) {
  for (var i=7; i; i--) {
    // Add a day
    date.setDate(date.getDate() + 1);

    // If a weekend or holiday, keep adding until notwhile(!(date.getDay()%6) || ukHolidays.indexOf(getISODate(date)) != -1) {
      date.setDate(date.getDate() + 1);
    }
  }
  return date;
}

// Add 7 working days to todayvar d = newDate();
console.log('Add 7 working days to ' + d.toString() + 
            '\nResult: ' + add7WorkingDays(d).toString());

Post a Comment for "Set Date To 7 Working Days From Today (excluding Weekends And Public Holidays)"