UNPKG

@thi.ng/date

Version:

Datetime types, relative dates, math, iterators, composable formatters, locales

23 lines (22 loc) 743 B
import { DAYS_IN_MONTH, DAYS_IN_MONTH_OFFSET } from "./api.js"; import { isLeapYear } from "./checks.js"; const daysInMonth = (year, month) => { const days = DAYS_IN_MONTH[month]; return days + ~~(month === 1 && isLeapYear(year)); }; const dayInYear = (y, m, d) => DAYS_IN_MONTH_OFFSET[m] + d + ~~(m > 1 && isLeapYear(y)); const weekInYear = (y, m, d) => { const start = new Date(Date.UTC(y, 0, 1)).getDay() || 7; if (!m) { if (start === 5 && d < 4) return 53; if (start === 6 && d < 3) return 52 + ~~isLeapYear(y - 1); if (start === 7 && d < 2) return 52; } const offset = (start < 5 ? 8 : 15) - start; return Math.ceil((dayInYear(y, m, d) - offset) / 7 + 1); }; export { dayInYear, daysInMonth, weekInYear };