UNPKG

date-limits

Version:

Check if a date is before a flexible limit.

27 lines (26 loc) 931 B
export function createUTCDate(year, month, day) { return new Date(Date.UTC(year, month - 1, day, 23, 59, 59, 999)); } export function isValidDate(date, year, month, day) { return date.getUTCFullYear() === year && date.getUTCMonth() + 1 === month && date.getUTCDate() === day; } 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]; } export 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; }