How to calculate the number of working days between two dates in JavaScript using moment.js. I have a working formula that calculate these days, but the formula does not sat
By using momentjs, it's possible this way:
private calculateWorkdays(startDate: Date, endDate: Date): number {
// + 1 cause diff returns the difference between two moments, in this case the day itself should be included.
const totalDays: number = moment(endDate).diff(moment(startDate), 'days') + 1;
const dayOfWeek = moment(startDate).isoWeekday();
let totalWorkdays = 0;
for (let i = dayOfWeek; i < totalDays + dayOfWeek; i++) {
if (i % 7 !== 6 && i % 7 !== 0) {
totalWorkdays++;
}
}
return totalWorkdays;
}