UNPKG

@burglekitt/gmt

Version:

Temporal-based date and time utilities with timezone support and polyfill integration

25 lines (24 loc) 670 B
import { Temporal } from "@js-temporal/polyfill"; /** * Return the current year from the Unix timestamp in UTC as a zero-padded string. * * - Uses Temporal.Now.instant() converted to UTC zoned date time. * - Returns zero-padded string to 4 digits. * - Returns "" on failure. * * @returns current year string (zero-padded to 4 digits) or "" on failure * * @example getUnixYear() // "2024" * @example getUnixYear() // "" (on failure) */ export function getUnixYear() { try { return Temporal.Now.instant() .toZonedDateTimeISO("UTC") .year.toString() .padStart(4, "0"); } catch { return ""; } }