UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

40 lines (38 loc) • 2.01 kB
import { isoDateString } from "../../internal/epochDay.js"; import { isValidDayOfWeek } from "../../internal/isValidDayOfWeek.js"; import { isWeekdayOrdinal, nthWeekdayEpochDay, ordinalNumber } from "../../internal/nthWeekday.js"; // src/calendar/rules/nthWeekdayOfMonth.ts /** Years Temporal's date range reaches (-271821-04-19 to +275760-09-13); beyond them epoch-day arithmetic overflows. */ const MIN_YEAR = -271821; const MAX_YEAR = 275760; /** * The `n`-th `weekday` of a month, counted from the start (`1` to `5`) or from the end (`-1` to * `-5`; `"last"` is `-1`), as RFC 5545 §3.3.10 counts a `BYDAY` ordinal in a monthly rule. * * - The ISO (proleptic Gregorian) calendar only. * - A fifth occurrence a month does not have (a fifth Friday of February 2026) gives `""`: the * date is skipped, never clamped to the fourth. * - A date outside Temporal's range (-271821-04-19 to +275760-09-13) gives `""`. * - Never throws. * * @param year ISO year, an integer * @param month ISO month, 1 to 12 * @param weekday ISO weekday, Monday 1 to Sunday 7 * @param n which occurrence: `1`..`5`, `-1`..`-5` or `"last"` * @returns ISO PlainDate string of the occurrence, or `""` when it does not exist or an argument is invalid * * @example nthWeekdayOfMonth(2026, 3, 7, "last") // "2026-03-29" — the last Sunday of March * @example nthWeekdayOfMonth(2026, 1, 1, 3) // "2026-01-19" — the third Monday of January * @example nthWeekdayOfMonth(2026, 2, 5, 5) // "" — February 2026 has only four Fridays * @example nthWeekdayOfMonth(2026, 13, 1, 1) // "" — invalid month */ function nthWeekdayOfMonth(year, month, weekday, n) { if (!Number.isInteger(year) || year < MIN_YEAR || year > MAX_YEAR || !Number.isInteger(month) || month < 1 || month > 12 || !isValidDayOfWeek(weekday) || !isWeekdayOrdinal(n)) return ""; try { const day = nthWeekdayEpochDay(year, month, weekday, ordinalNumber(n)); return day === null ? "" : isoDateString(day); } catch { return ""; } } export { nthWeekdayOfMonth };