@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.
33 lines (31 loc) • 957 B
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { systemTimeZone } from "../../internal/resolveLocale.js";
import { nowPlainDateTimeISO } from "../../internal/temporalCompat/clock.js";
// src/plain/get/getWeekOfYear.ts
/**
* Get the ISO week number of the current date.
*
* - Uses nowPlainDateTimeISO() 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)
*/
function getWeekOfYear() {
const timeZone = systemTimeZone();
if (!timeZone) return null;
try {
const now = nowPlainDateTimeISO(timeZone);
return Temporal.PlainDate.from({
year: now.year,
month: now.month,
day: now.day
}).weekOfYear ?? null;
} catch {
return null;
}
}
export { getWeekOfYear };