@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
27 lines (26 loc) • 751 B
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import { isValidZonedDateTime } from "../validate/index.js";
/**
* Return the year for a given ISO 8601 zoned datetime string.
*
* - Returns string for year.
* - Returns "" for invalid input.
*
* @param value ISO zoned datetime string
* @returns Year as string or "" on invalid input
*
* @example parseYearFromZoned("2024-03-15T14:30:45.123+00:00[UTC]") // "2024"
* @example parseYearFromZoned("invalid") // ""
*/
export function parseYearFromZoned(value) {
if (!isValidZonedDateTime(value)) {
return "";
}
try {
const zonedDateTime = Temporal.ZonedDateTime.from(value);
return zonedDateTime.year.toString();
}
catch {
return "";
}
}