javascript date calculations ignore weekends code example

Example: javascript date calculations ignore weekends

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);
}

function calc() {
  let start = document.querySelector('#startDate').value,
      end = document.querySelector('#endDate').value,
      result = getBusinessDateCount(new Date(start), new Date(end));
  document.querySelector('#result').value = result;
}

Tags:

Misc Example