date-limits
Version:
Check if a date is before a flexible limit.
33 lines (32 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.standardizeDay = exports.isValidDate = exports.createUTCDate = void 0;
function createUTCDate(year, month, day) {
return new Date(Date.UTC(year, month - 1, day, 23, 59, 59, 999));
}
exports.createUTCDate = createUTCDate;
function isValidDate(date, year, month, day) {
return date.getUTCFullYear() === year && date.getUTCMonth() + 1 === month && date.getUTCDate() === day;
}
exports.isValidDate = isValidDate;
const MONTH_LENGTHS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function _isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
function _getMonthLength(year, month) {
if (_isLeapYear(year) && month === 2)
return 29;
return MONTH_LENGTHS[month - 1];
}
function standardizeDay(year, month, day) {
if (day === 0) {
throw new Error(`Day limit cannot have a value of zero.`);
}
const lastDay = _getMonthLength(year, month);
if (day < 0) {
day = -(-(day + 1) % lastDay);
return lastDay - day;
}
return day;
}
exports.standardizeDay = standardizeDay;