javascript date calculations ignore weekends

Solutions on MaxInterview for javascript date calculations ignore weekends by the best coders in the world

showing results for - "javascript date calculations ignore weekends"
Eleonora
07 Feb 2016
1function getBusinessDateCount (startDate, endDate) {
2    var elapsed, daysBeforeFirstSaturday, daysAfterLastSunday;
3    var ifThen = function (a, b, c) {
4        return a == b ? c : a;
5    };
6
7    elapsed = endDate - startDate;
8    elapsed /= 86400000;
9
10    daysBeforeFirstSunday = (7 - startDate.getDay()) % 7;
11    daysAfterLastSunday = endDate.getDay();
12
13    elapsed -= (daysBeforeFirstSunday + daysAfterLastSunday);
14    elapsed = (elapsed / 7) * 5;
15    elapsed += ifThen(daysBeforeFirstSunday - 1, -1, 0) + ifThen(daysAfterLastSunday, 6, 5);
16
17    return Math.ceil(elapsed);
18}
19
20function calc() {
21  let start = document.querySelector('#startDate').value,
22      end = document.querySelector('#endDate').value,
23      result = getBusinessDateCount(new Date(start), new Date(end));
24  document.querySelector('#result').value = result;
25}