Create an instance from an ISO string, year and month numbers, or an existing date with yearMonth(), or read card-expiry strings month-first with monthYear(). Instances are immutable, so every method that changes a value returns a new one.
Places a whole day number from 1 through 31 in this month and returns a CalendarDate. Day 31 clamps to the final day of shorter months such as February or April, while numbers outside 1 to 31 throw a RangeError.
Compare year-months chronologically or check whether a calendar day falls inside the month. Relational operators like < throw an error, while arrays sort cleanly with compare() and boundary helpers.
compare()
ym.compare(other): -1 | 0 | 1
Orders this year-month against another, returning negative, zero, or positive numbers for sorting. Passing a full date or timestamp compares against the month it falls in.
Parameters
otherYearMonthInputrequired
Another YearMonth or any input accepted by yearMonth().
Returns
-1 | 0 | 1
Returns -1 when earlier, 0 when equal, and 1 when later.
ym.until(other): Durationym.until(other, unit): number
Measures the span from this month to another, returning a negative duration when the target is earlier. The returned Duration remembers the start of this month so months and years can be totaled in any unit.
Parameters
otherYearMonthInputrequired
Target year-month to measure toward.
unitUnit
Unit name to return the total span in as a floating-point number.
Returns
Duration | number
A Duration of years and months, or a number when unit is given.
Formats the year and month using named presets, Intl options objects, or built-in token patterns such as 'MM/YY'. Shared options objects drop day, clock, and zone keys automatically.
Standard and localized presets print only the year and month, and custom formats registered in configure() work here too. See Formatting for the full preset and token tables.
Parameters
specFormatSpecdefault 'medium'
Preset name such as 'medium' or 'long', Intl options object, or token pattern.
localeLocale
BCP 47 locale tag overriding this month's or the configured locale for one call.
Reads the locale carried on this month, or returns a copy formatted in another locale across text previews, presets, and every value derived from it. Passing an empty tag clears the carried locale, and lang() works as an alias.
Parameters
tagLocale | null | undefined
BCP 47 locale tag to carry forward, or null to clear an existing tag.
Returns
YearMonth | Locale | undefined
The active tag when called with no arguments, or a new YearMonth carrying tag.
const may = yearMonth('2026-05').locale('ru');may.locale(); // 'ru'may.format('long'); // 'май 2026 г.'may.on(1).format('long'); // '1 мая 2026 г.'