Calculate number of working days

Calculate number of working days
0.0 0


Can any one please help me to write code in JS which will calculate no. of working days when start and end date are provided, excluding the weekends.


Why don’t you share the code you’ve written to do this and the issues you’ve run into and perhaps someone can help further?

    <h2>Number of days</h2>


<td>Start date (mm/dd/yyyy)</td>
<td> <input type="text"  name="start" id="start"></td>



<td>End date (mm/dd/yyyy)</td>
<td> <input type="text"  name="end" id="end"></td>


<td><button type="button" id="calculate" onclick = "calculate()">Calculate</button></td>

<div id="result"></div>


function calculate() {

var stdate = document.getElementById("start").value;
var stdate1=  stdate.split("/");	
var endate = document.getElementById("end").value;
var endate1= endate.split("/");	

for(var i=0; stdate1<endate1; ) {
if( stdate.getDay() >0 && endate.getDay() <6) i++;
stdate.setDate(stdate.getDate()+1) ;
//document.getElementById("result").innerHTML = "Toggled String is " + str2;



You can use the .getDay() method on the date object.

The date object takes 3 parameters - year, month, day - in that order. You would have to return a date object like this, with the months going from 0 (jan) -11 (dec)

const dateToCheck = new Date(yyyy, mm, dd)

Then with the .getDay() method, you check if it’s a weekend, with sunday === 0 and moving up from there

dateToCheck.getDay() //  0 (sunday) - 6 (saturday)