Will the below code work for adding Weekdays in all possible scenarios? eg, If I add 4 days to a "Thursday", the result should be next "Wednesday". Adding 1 day to a "Friday", "Saturday" or "Sunday" should be the next "Monday".
I am trying to get the nth working day (weekday) after a given day.
var addOneDay=function(date) {
var result = new Date(date.getTime());
result.setDate(result.getDate() + 1);
return result;
};
var addWeekDays = function(date,days) {
var result = new Date(date.getTime());
for (var i = 0; i < days; i++) {
result = addOneDay(result);
if (result.getDay()=== 6 || result.getDay()=== 0) i--;
};
return result;
};
var date = new Date(2016,1,18);
console.log(addWeekDays(date,4));