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.

33 lines (31 loc) • 1.37 kB
import { plainYearMonthOf } from "../../internal/inputChecks.js"; import { yearMonthString } from "../../internal/yearMonth.js"; // src/plain/parse/parseYearMonth.ts /** * Return the canonical `YYYY-MM` form of an ISO year-month string (`±YYYYYY-MM` outside * 0000-9999). * * - Reads `YYYY-MM` and `±YYYYYY-MM`; writes the shortest form. The reference day is never * written. * - The extended form only: `202610` is refused, as `isValidDate` refuses `20241225`. * - A date such as `2026-10-01` is not a year-month: use `parseYearMonthFromDate`. * - The range is Temporal's: `-271821-04` to `+275760-09`. * - Accepts no annotation, as `isValidYearMonth` does: any bracket returns "". * - Returns "" for invalid input. * * @param value ISO year-month string * @returns the `YYYY-MM` string, or "" on invalid input * * @example parseYearMonth("2026-10") // "2026-10" * @example parseYearMonth("+002026-10") // "2026-10" * @example parseYearMonth("-000001-12") // "-000001-12" * @example parseYearMonth("2026-10[u-ca=ISO8601]") // "" (no annotation of any kind) * @example parseYearMonth("+275760-10") // "" * @example parseYearMonth("202610") // "" * @example parseYearMonth("2026-10-01") // "" */ function parseYearMonth(value) { const yearMonth = plainYearMonthOf(value); return yearMonth === null ? "" : yearMonthString(yearMonth); } export { parseYearMonth };