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) • 904 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.splitDateRangeByMonth = void 0;
/**
* Splits a date range into an array of month-range chunks.
* @author @dailker
* @param {Date} start
* @param {Date} end
* @returns {Array<{start: Date, end: Date}>}
*/
function splitDateRangeByMonth(start, end) {
const result = [];
let current = new Date(start.getFullYear(), start.getMonth(), 1);
while (current <= end) {
const monthEnd = new Date(current.getFullYear(), current.getMonth() + 1, 0, 23, 59, 59, 999);
result.push({
start: new Date(Math.max(current.getTime(), start.getTime())),
end: new Date(Math.min(monthEnd.getTime(), end.getTime())),
});
current.setMonth(current.getMonth() + 1, 1);
}
return result;
}
exports.splitDateRangeByMonth = splitDateRangeByMonth;