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;
}
Also in JavaScript:
- javascript randomly shuffle array
- javascript subtract years from date
- disable autocomplete in react fields
- html video tag autoplay not working
- spread operator javascript
- checkbox on click jquery
- js queryselector names
- component did mount mutation graphql
- react native activity
- javascript this
- redux toolkit reducer
- javascript remover acentos
- javascript dataurl to blob
- Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\].*|website\\node_modules\\.*|heapCapture\\bundle\.js|.*\\__tests__\\.*)$/: Unterminated character class
- javascript return string
- react sagas state
- if parameter is not number in js
- javascript cheat sheet
- jquery trigger link click
- mongodb js get id of inserted
- how to know actual scroll js
- w3schools ajax
- how the map function works javascript
- React Hooks append state