@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.
29 lines (27 loc) • 869 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.js";
// src/plain/calculate/weekOfYearForDate.ts
/**
* Return the ISO week number for a given ISO 8601 date string.
*
* - Uses Temporal.PlainDate.weekOfYear (Monday-based ISO weeks).
* - Returns null for invalid input.
*
* @param value ISO 8601 date string
* @returns Week number (1-53) or null on invalid input
*
* @example weekOfYearForDate("2024-01-01") // 1
* @example weekOfYearForDate("2024-01-07") // 1
* @example weekOfYearForDate("2024-01-08") // 2
* @example weekOfYearForDate("2024-12-31") // 1
* @example weekOfYearForDate("invalid") // null
*/
function weekOfYearForDate(value) {
if (!isValidDate(value)) return null;
try {
return Temporal.PlainDate.from(value).weekOfYear ?? null;
} catch {
return null;
}
}
export { weekOfYearForDate };