@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) • 992 B
JavaScript
import { systemTimeZone } from "../../internal/resolveLocale.js";
import { nowZonedDateTimeISO } from "../../internal/temporalCompat/clock.js";
import { isoYearString } from "../../internal/isoYearString.js";
// src/plain/get/getYear.ts
/**
* Return the current year as a zero-padded string in the system timeZone.
*
* - Uses nowZonedDateTimeISO() to get current year in system timezone.
* - Returns zero-padded string (e.g., "2024").
* - 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.
* - Returns "" when system timezone is unavailable.
*
* @returns current year string (zero-padded) or "" on invalid
*
* @example getYear() // "2024"
* @example getYear() // "" (when system timeZone unavailable)
*/
function getYear() {
const timeZone = systemTimeZone();
if (!timeZone) return "";
try {
return isoYearString(nowZonedDateTimeISO(timeZone).year);
} catch {
return "";
}
}
export { getYear };