Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add no. of days in a date to get next date(excluding weekends)

I have an date, i need to add no. of days to get future date but weekends should be excluded. i.e

input date = "9-DEC-2011";
No. of days to add  = '13';

next date should be "28-Dec-2011"

Here weekends(sat/sun) are not counted.

like image 211
Nishu Tayal Avatar asked Dec 09 '11 20:12

Nishu Tayal


People also ask

How do you add days to date including or excluding weekends?

Add business days excluding weekends with formula To add days excluding weekends, you can do as below: Select a blank cell and type this formula =WORKDAY(A2,B2), and press Enter key to get result. Tip: In the formula, A2 is the start date, B2 is the days you want to add.

How do I count days between excluding weekends in Excel?

=NETWORKDAYS(A2,B2) Then type Enter key, and you will count the number of workdays excluding Sundays and Saturdays between the two dates.


1 Answers

Try this

var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 13, 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++;
    }
}
alert(endDate);//You can format this date as per your requirement

Working Demo

like image 67
ShankarSangoli Avatar answered Sep 21 '22 18:09

ShankarSangoli