@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
33 lines (32 loc) • 974 B
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { getSystemTimeZone } from "../../zoned/get/getSystemTimeZone.js";
/**
* Get the ISO week number of the current date.
*
* - Uses Temporal.Now.plainDateTimeISO to get current date in system timezone.
* - Uses Temporal.PlainDate.weekOfYear for ISO week number.
* - Returns null when system timezone is unavailable.
*
* @returns The ISO week number (1-53), or null on invalid
*
* @example getWeekOfYear() // 15
* @example getWeekOfYear() // null (when system timeZone unavailable)
*/
export function getWeekOfYear() {
const timeZone = getSystemTimeZone();
if (!timeZone) {
return null;
}
try {
const now = Temporal.Now.plainDateTimeISO(timeZone);
const date = Temporal.PlainDate.from({
year: now.year,
month: now.month,
day: now.day,
});
return date.weekOfYear ?? null;
}
catch {
return null;
}
}