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.

32 lines (30 loc) • 961 B
import { Temporal } from "../../internal/temporal.js"; import { isValidDateTime } from "../validate/isValidDateTime.js"; // src/plain/calculate/weekOfYearForDateTime.ts /** * Return the ISO week number for a given ISO 8601 datetime string. * * - Uses Temporal.PlainDate.weekOfYear (Monday-based ISO weeks). * - Returns null for invalid input. * * @param value ISO 8601 datetime string * @returns Week number (1-53) or null on invalid input * * @example weekOfYearForDateTime("2024-01-01T12:00:00") // 1 * @example weekOfYearForDateTime("2024-01-08T00:00:00") // 2 * @example weekOfYearForDateTime("invalid") // null */ function weekOfYearForDateTime(value) { if (!isValidDateTime(value)) return null; try { const dateTime = Temporal.PlainDateTime.from(value); return Temporal.PlainDate.from({ year: dateTime.year, month: dateTime.month, day: dateTime.day }).weekOfYear ?? null; } catch { return null; } } export { weekOfYearForDateTime };