@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.
31 lines (29 loc) • 1.16 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDate } from "../validate/isValidDate.js";
import { yearMonthString } from "../../internal/yearMonth.js";
// src/plain/parse/parseYearMonthFromDate.ts
/**
* Return the year-month (`YYYY-MM`) of an ISO 8601 date string.
*
* - The day is dropped, so `2026-10-01` and `2026-10-31` give the same year-month.
* - Only the ISO calendar is read; a Hebrew or Islamic date gives `""`.
* - Returns "" for invalid input.
*
* @param value ISO date string
* @returns The canonical `YYYY-MM` string (`±YYYYYY-MM` outside 0000-9999), or "" on invalid input
*
* @example parseYearMonthFromDate("2026-10-01") // "2026-10"
* @example parseYearMonthFromDate("2026-10-31") // "2026-10"
* @example parseYearMonthFromDate("+275760-09-13") // "+275760-09"
* @example parseYearMonthFromDate("2023-02-29") // ""
* @example parseYearMonthFromDate("2026-10-01[u-ca=hebrew]") // ""
*/
function parseYearMonthFromDate(value) {
if (!isValidDate(value)) return "";
try {
return yearMonthString(Temporal.PlainDate.from(value).toPlainYearMonth());
} catch {
return "";
}
}
export { parseYearMonthFromDate };