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.

30 lines (28 loc) • 1.1 kB
import { isoYearString } from "../../internal/isoYearString.js"; import { frameNowWallClock, zoneFrame } from "../../internal/zoneFrame.js"; // src/zoned/get/getZonedYear.ts /** * Return the current year for the specified IANA timeZone. * * - Uses nowZonedDateTimeISO() to get the current time. * - Validation is performed on the timezone. * - The year is zero-padded to 4 digits. A year outside 0000–9999 is written with a sign and six * digits (`+010000`, `-000005`), as Temporal writes it at the head of a date. * * @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 year string (zero-padded to 4 digits) or "" on invalid input * * @example getZonedYear("Africa/Johannesburg") // "2024" * @example getZonedYear("invalid") // "" */ function getZonedYear(ianaTimezone) { const frame = zoneFrame(ianaTimezone); if (frame === null) return ""; try { return isoYearString(frameNowWallClock(frame).year); } catch { return ""; } } export { getZonedYear };