30 lines
753 B
TypeScript
30 lines
753 B
TypeScript
import { Moment } from "moment-timezone";
|
|
import Holidays from "date-holidays";
|
|
|
|
const holidays = new Holidays()
|
|
|
|
export function countWorkingWeeksInRange(start: Moment, end: Moment) {
|
|
const iterator = start.clone();
|
|
|
|
let i = 0;
|
|
while (iterator.isBefore(end)) {
|
|
iterator.add(1, 'day');
|
|
const holiday = holidays.isHoliday(iterator.toDate());
|
|
|
|
const isHoliday = holiday && holiday.type == "public";
|
|
const isWeekend = [6, 7].includes(iterator.isoWeekday())
|
|
|
|
if (isHoliday || isWeekend) {
|
|
continue;
|
|
}
|
|
|
|
i++;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
export function computeWorkingHours(start: Moment, end: Moment, perDay: number = 8) {
|
|
return countWorkingWeeksInRange(start, end) * perDay;
|
|
}
|