Calculate number of working days

Calculate number of working days
0.0 0

#1

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.


#2

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?
./H.B.


#3
<body>
	
    <center>
    
    <h2>Number of days</h2>
<table>

<tr>

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

</tr>

<tr>

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

</tr>

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



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

<script>

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) ;
}
alert(i);
//document.getElementById("result").innerHTML = "Toggled String is " + str2;
}

</script>
    
</body>
</html>

#4

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)