Skipping saturdays and sundays in javascript and adding non repeated entries to mysql database
<p>I am writing a rest api using Nodejs which allows users to apply leaves .
The inputs are </p>

<ol>
<li>Number of days as(noOfLeaves).</li>
<li>From Date( where I use the day to decide if its weekend and also for skipping month.</li>
<li>No of days in the month(for skipping to next month in case).
I get the month and start day using fromDate.</li>
</ol>

<p>I am implementing a logic which checks two conditions.
a) Skip to weekdays if its a weekend(sat/sun).
b) Skip to next month if its month end. </p>

<p>My logic below skips weekends but repeats the weekdays. 
What might be the issue, any better approach to achieve this.</p>

<pre><code>var splitDat = '2016-05-27'.split("-", 3);
var nxtMonthDay = 1;
var j = 0;
var noOfDaysForThisMonth = new Date(parseInt(splitDat[0]), parseInt(splitDat[1]), 0).getDate();
var noOfLeaves = 5;
for (var i = 0; i &lt; noOfLeaves; i++) {
    var inputYear = parseInt(splitDat[0]);
    var inputMonth = parseInt(splitDat[1]);
    var inputDay = parseInt(splitDat[2]);
    if (parseInt(inputDay) + i &gt; noOfDaysForThisMonth) {
        inputMonth = inputMonth + 1
        inputDay = nxtMonthDay + j;
        j++;
    } else {
        inputDay = inputDay + i;
    }
    var dayVal = new Date(inputYear, (parseInt(inputMonth) - 1), inputDay).getDay();
    // console.log('Day value : ' + dayVal);
    if (dayVal == 6) {
        if ((inputDay + 2) &gt; noOfDaysForThisMonth) {
            inputDay = nxtMonthDay;
        } else {
            inputDay = inputDay + 2;
        }
        // console.log('Saturday');
    } else if (dayVal == 0) {
        // console.log('Sunday');
        if ((inputDay + 3) &gt; noOfDaysForThisMonth) {
            if ((inputDay + 2) &gt; noOfDaysForThisMonth) {
                inputDay = nxtMonthDay;
            } else {
                inputDay = inputDay + 2;
            }
        } else {
            inputDay = inputDay + 3;
        }
    }
    console.log(inputYear+'-'+ inputMonth+'-'+ inputDay);
    //this is a function call to create a leave entry in the database using year-month-day
    ApplyLeave(inputYear, inputMonth, inputDay);

}
</code></pre>

<p>This gives output as </p>

<ul>
<li>2016-5-27</li>
<li>2016-5-30</li>
<li>2016-5-31</li>
<li>2016-5-30</li>
<li>2016-5-31</li>
</ul>

<p>I am expecting an output as</p>

<ul>
<li>2016-5-27</li>
<li>2016-5-30</li>
<li>2016-5-31</li>
<li>2016-6-01</li>
<li>2016-6-02</li>
</ul>

<p>Can someone tell me if my logic is correct and if not how to do this?</p>