everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
25 lines (24 loc) • 792 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateBusinessHoursBetween = void 0;
/**
* Calculates business hours elapsed between two datetimes.
* @author @dailker
* @param {Date} start
* @param {Date} end
* @param {number} [workStart=9]
* @param {number} [workEnd=17]
* @returns {number}
*/
function calculateBusinessHoursBetween(start, end, workStart = 9, workEnd = 17) {
let hours = 0;
let d = new Date(start);
while (d < end) {
if (d.getDay() !== 0 && d.getDay() !== 6 && d.getHours() >= workStart && d.getHours() < workEnd) {
hours++;
}
d.setHours(d.getHours() + 1);
}
return hours;
}
exports.calculateBusinessHoursBetween = calculateBusinessHoursBetween;