@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.
28 lines (26 loc) • 951 B
JavaScript
import { frameNowWallClock, zoneFrame } from "../../internal/zoneFrame.js";
// src/zoned/get/getZonedWeekOfYear.ts
/**
* Return the current week of year for the specified IANA timeZone.
*
* - Uses nowZonedDateTimeISO() to get the current time.
* - Uses Temporal.PlainDate.weekOfYear for ISO week number.
* - Validation is performed on the timezone.
*
* @param ianaTimezone IANA name or UTC offset: a time zone identifier (`+05:30`, `+0530`, `-08`) or
* a stored offset (`±HH:MM[:SS]`, what `getTimeZoneOffset` returns)
* @returns current week number (1-53) or null on invalid input
*
* @example getZonedWeekOfYear("America/Sao_Paulo") // 9
* @example getZonedWeekOfYear("invalid") // null
*/
function getZonedWeekOfYear(ianaTimezone) {
const frame = zoneFrame(ianaTimezone);
if (frame === null) return null;
try {
return frameNowWallClock(frame).toPlainDate().weekOfYear ?? null;
} catch {
return null;
}
}
export { getZonedWeekOfYear };