Skip to content

parseYearMonthWithPattern

parseYearMonthWithPattern(value: string, pattern: string, locale?: string | string[], options?: TwoDigitYearOptions): string

Parse a year-month string against a caller-supplied token pattern (e.g. "MM/yyyy", "MMMM yyyy") and return it as an ISO year-month string, YYYY-MM.

  • Decoding, not display. This reads a known, fixed producer format. A pattern with a day, weekday or time token returns "" — use parseDateWithPattern for a full date.

  • The pattern needs both a year and a month, otherwise "".

  • Digits are ASCII, as in the other pattern parsers. ### Token table

    TokenFieldWidthRangeNotes
    yyyyyear4 digits0000–9999
    yyyear2 digits—Read through options.yearWindow; with no window the result is ""
    MMmonth2 digits01–12
    Mmonth1-2 digits1–12
    MMMMmonth name (long)locale—Reads the format-context name (“октября”) or the stand-alone name (“октябрь”); UTS #35 Part 4, Parsing Dates and Times
    MMMmonth name (short)locale—Reads either context, as MMMM
    LLmonth2 digits01–12Stand-alone context; read as MM
    Lmonth1-2 digits1–12Stand-alone context; read as M
    LLLLmonth name (long)locale—Same names as MMMM
    LLLmonth name (short)locale—Same names as MMM
    GG / GGGGeralocale—Qualifies the year: 1 BC is year 0000
  • Literal text follows UTS #35: text in 'single quotes' is a literal, '' is one literal quote, and a quote that never closes makes the pattern malformed ("").

  • Month names match case-insensitively. A -u-ca- locale extension is overridden to Gregorian.

  • Name tokens are locale-aware. If locale is omitted (or an empty list) and the pattern uses one, GMT reads English names in "en". An ill-formed tag returns "".

ParameterTypeDescription
valuestringThe string to decode (e.g. “10/2026”)
patternstringThe token pattern describing value’s shape (e.g. “MM/yyyy”)
localestring | string[]Optional BCP 47 locale tag, or a preference list of tags, for name-based tokens (default “en”)

options

The hundred-year window a yy token resolves in

Type: TwoDigitYearOptions.

OptionTypeDefaultDescription
yearWindow?number | "rolling"None. A value with a two-digit year returns the sentinel: no standard says which century it belongs to, so the caller states it.The hundred-year window a two-digit year falls in. A number is the first year of a fixed window, an integer from 0 to 9900: 2000 reads 00–99 as 2000–2099; 1950 reads 50–99 as 1950–1999 and 00–49 as 2000–2049. A fixed window gives the same answer in any year, so it is the form for stored data, archives and anything re-processed later. "rolling" is the hundred years around today: from 50 years before the current UTC calendar year to 49 years after it (in 2026, 1976–2075). It is the form for live messages and never needs maintenance, but it gives a different answer for the same stored value as years pass. Any other value returns the function’s sentinel.

the YYYY-MM string, or "" on no match, malformed pattern, or invalid input

import { parseYearMonthWithPattern } from "@northguild/gmt/plain/parse";
parseYearMonthWithPattern("10/2026", "MM/yyyy") // "2026-10"
output 

plain/parse/parseYearMonthWithPattern.ts

Code examples: MIT-0. Packages: MIT. Third-party data and components: their own licences, credited where they appear. Everything else:© 2026 NorthGuild, all rights reserved.