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.

29 lines (27 loc) • 981 B
import { plainDateTimeOf } from "../../internal/inputChecks.js"; import { isoYearString } from "../../internal/isoYearString.js"; // src/plain/parse/parseYearFromDateTime.ts /** * Return the year for a given ISO 8601 datetime string. * * - Returns the year as Temporal writes it: four digits (`"2024"`, `"0005"`), or a sign and six * digits outside 0000–9999 (`"+010000"`, `"-000005"`). * - Returns "" for invalid input. * * @param value ISO datetime string * @returns the year as four digits, or a sign and six digits outside 0000–9999, or "" on invalid input * * @example parseYearFromDateTime("2024-03-15T12:30:00") // "2024" * @example parseYearFromDateTime("0005-06-01T12:30:00") // "0005" * @example parseYearFromDateTime("invalid") // "" */ function parseYearFromDateTime(value) { const dateTime = plainDateTimeOf(value); if (dateTime === null) return ""; try { return isoYearString(dateTime.year); } catch { return ""; } } export { parseYearFromDateTime };