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.

34 lines (32 loc) • 1.25 kB
import { Temporal } from "../../internal/temporal.js"; import { isValidDate } from "../validate/isValidDate.js"; // src/plain/calculate/getWeekYear.ts /** * Return the ISO 8601 week-numbering year `value` belongs to. * * - Distinct from `value`'s calendar year — late-December/early-January * dates can belong to a different ISO week-year than their calendar * year (e.g. 2024-12-30 is a Monday in ISO week 1 of **2025**). * Pair this with `weekOfYearForDate` when bucketing by week: a week * number alone is ambiguous without the week-year it belongs to. * - Uses `Temporal.PlainDate.yearOfWeek` (ISO weeks: Monday-start, week 1 * contains the year's first Thursday). * - Returns null on invalid input. * * @param value ISO PlainDate string * @returns ISO week-numbering year, or null on invalid input * * @example getWeekYear("2024-06-15") // 2024 * @example getWeekYear("2024-12-30") // 2025 (Monday of ISO week 1, 2025) * @example getWeekYear("2021-01-01") // 2020 (belongs to ISO week 53 of 2020) * @example getWeekYear("invalid") // null */ function getWeekYear(value) { if (!isValidDate(value)) return null; try { return Temporal.PlainDate.from(value).yearOfWeek ?? null; } catch { return null; } } export { getWeekYear };