Here is a classic piece of javascript:
"use strict";
var daysOff = [0, 6],
weekdays = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
// val represents a comma-separated month and year
// e.g.: 7,2017 or 04, 2015
// it can also be just a number (5, 9, etc.) in which case the code uses the current year
function calculateWorkingDays(val) {
if(!val) { return false; }
var workingCount, result = {start: null, end: null, days: []};
val = val.split(',');
// subtracting 1 as months are zero-based
val[0] = parseInt(val[0]) - 1;
if(val[0] > 11) { val[0] = val[0]%12; }
// if year is not provided, use the current year
if(typeof val[1] == 'undefined') {
val[1] = new Date();
val[1] = val[1].getFullYear();
}
// get the first day of the current and next month
result.start = new Date(val[1], val[0]);
result.end = new Date(val[1], val[0] + 1);
// keep only the day number (0 - Sunday, ... 6 - Saturday)
result.start = result.start.getDay();
result.end = result.end.getDay();
// add 28 or 35 to result.end depending on whether it's less than result.start
if(result.end < result.start) { result.end += 7; }
result.end += 28;
// calculating working days
var i = result.start;
while(i < result.end) {
workingCount = Math.floor(i/7);
if(typeof result.days[workingCount] == 'undefined') { result.days[workingCount] = 0; }
if(daysOff.indexOf(i%7) == -1) { ++result.days[workingCount]; }
++i;
}
// generating the output
result.start = weekdays[result.start] + 'day';
result.end = weekdays[(result.end-1)%7] + 'day';
console.log(result);
return result;
}