function getBusinessDateCount (startDate, endDate) {
var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;
var ifThen = function (a, b, c) {
return a == b ? c : a;
};
elapsed = endDate - startDate;
elapsed /= 86400000;
daysBeforeFirstSunday = (7 - startDate.getDay()) % 7;
daysAfterLastSunday = endDate.getDay();
elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);
elapsed = (elapsed / 7) * 5;
elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);
return Math.ceil(elapsed);
/* 2015-04-29 til 2015-04-29 will return 1
if you want it to return 0 instead 1:
change Math.ceil() to parseInt() in last line */
}
var date1 = new Date(1999, 12, 31);
var date2 = new Date(); // now
print( getBusinessDateCount(date1, date2) );
ZnVuY3Rpb24gZ2V0QnVzaW5lc3NEYXRlQ291bnQgKHN0YXJ0RGF0ZSwgZW5kRGF0ZSkgewogICAgdmFyIGVsYXBzZWQsIGRheXNCZWZvcmVGaXJzdFNhdHVyZGF5LCBkYXlzQWZ0ZXJMYXN0U3VuZGF5OwogICAgdmFyIGlmVGhlbiA9IGZ1bmN0aW9uIChhLCBiLCBjKSB7CiAgICAgICAgcmV0dXJuIGEgPT0gYiA/IGMgOiBhOwogICAgfTsKICAgIAogICAgZWxhcHNlZCA9IGVuZERhdGUgLSBzdGFydERhdGU7CiAgICBlbGFwc2VkIC89IDg2NDAwMDAwOwogICAgCiAgICBkYXlzQmVmb3JlRmlyc3RTdW5kYXkgPSAoNyAtIHN0YXJ0RGF0ZS5nZXREYXkoKSkgJSA3OwogICAgZGF5c0FmdGVyTGFzdFN1bmRheSA9IGVuZERhdGUuZ2V0RGF5KCk7CiAgICAKICAgIGVsYXBzZWQgLT0gKGRheXNCZWZvcmVGaXJzdFN1bmRheSArIGRheXNBZnRlckxhc3RTdW5kYXkpOwogICAgZWxhcHNlZCA9IChlbGFwc2VkIC8gNykgKiA1OwogICAgZWxhcHNlZCArPSBpZlRoZW4oZGF5c0JlZm9yZUZ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