Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  170] [ 1]  / answers: 1 / hits: 25609  / 8 Years ago, tue, november 22, 2016, 12:00:00

How can I use JavaScript to add working days (i.e. Mon - Friday) automatically adding weekends where necessary?


So if I were to add 5 working days to today (Tue. 22nd Nov. 2016) the result should be "Tue. 29th Nov. 2016" and not "Sun. 27th Nov. 2016".


More From » javascript

 Answers
325

It is possible to use Date's setDate function (in combination with getDate) to add days onto a date i.e. -



var myDate = new Date(); // Tue 22/11/2016
myDate.setDate(myDate.getDate() + 3); // Fri 25/11/2016


So once you've calculated the number of weekend days within the workdays period you can add that and the required number of workdays to the start date to get the final date.



This function should work though obviously this will not take account of national holidays -



function addWorkDays(startDate, days) {
if(isNaN(days)) {
console.log(Value provided for days was not a number);
return
}
if(!(startDate instanceof Date)) {
console.log(Value provided for startDate was not a Date object);
return
}
// Get the day of the week as a number (0 = Sunday, 1 = Monday, .... 6 = Saturday)
var dow = startDate.getDay();
var daysToAdd = parseInt(days);
// If the current day is Sunday add one day
if (dow == 0)
daysToAdd++;
// If the start date plus the additional days falls on or after the closest Saturday calculate weekends
if (dow + daysToAdd >= 6) {
//Subtract days in current working week from work days
var remainingWorkDays = daysToAdd - (5 - dow);
//Add current working week's weekend
daysToAdd += 2;
if (remainingWorkDays > 5) {
//Add two days for each working week by calculating how many weeks are included
daysToAdd += 2 * Math.floor(remainingWorkDays / 5);
//Exclude final weekend if remainingWorkDays resolves to an exact number of weeks
if (remainingWorkDays % 5 == 0)
daysToAdd -= 2;
}
}
startDate.setDate(startDate.getDate() + daysToAdd);
return startDate;
}

//And use it like so (months are zero based)
var today = new Date(2016, 10, 22);
today = addWorkDays(today, 5); // Tue Nov 29 2016 00:00:00 GMT+0000 (GMT Standard Time)


It could also be added to the Date prototype -



Date.prototype.addWorkDays = function (days) {
if(isNaN(days)) {
console.log(Value provided for days was not a number);
return
}

// Get the day of the week as a number (0 = Sunday, 1 = Monday, .... 6 = Saturday)
var dow = this.getDay();
var daysToAdd = parseInt(days);
// If the current day is Sunday add one day
if (dow == 0) {
daysToAdd++;
}
// If the start date plus the additional days falls on or after the closest Saturday calculate weekends
if (dow + daysToAdd >= 6) {
//Subtract days in current working week from work days
var remainingWorkDays = daysToAdd - (5 - dow);
//Add current working week's weekend
daysToAdd += 2;
if (remainingWorkDays > 5) {
//Add two days for each working week by calculating how many weeks are included
daysToAdd += 2 * Math.floor(remainingWorkDays / 5);
//Exclude final weekend if the remainingWorkDays resolves to an exact number of weeks
if (remainingWorkDays % 5 == 0)
daysToAdd -= 2;
}
}
this.setDate(this.getDate() + daysToAdd);
};

//And use it like so (months are zero based)
var today = new Date(2016, 10, 22)
today.addWorkDays(5); // Tue Nov 29 2016 00:00:00 GMT+0000 (GMT Standard Time)

[#59965] Sunday, November 20, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kirstendevanb

Total Points: 585
Total Questions: 93
Total Answers: 94

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
;