yearMonth()
yearMonth(input, options?): YearMonth
yearMonth(input, zone?): YearMonth
yearMonth(year, month): YearMonthCreates a YearMonth from a year-first string, year and 1-indexed month numbers, a fields object, or any date or timestamp with its day dropped. Missing inputs throw a type error instead of defaulting to the current month.
import { datetime, yearMonth } from 'native-dates';
const fromIso = yearMonth('2026-11');
const fromNumbers = yearMonth(2026, 11);
const fromFields = yearMonth({ year: 2026, month: 11 });
const fromDate = yearMonth('2026-11-16');
const fromInstant = yearMonth(datetime('2026-11-01T02:00:00Z'), 'America/Los_Angeles');
fromNumbers.toString(); // '2026-11'
fromInstant.toString(); // '2026-10'Parameters
ISO year-month string, fields object, date or timestamp, or calendar year.
Time zone or options object when reading a timestamp or loose string, or 1-indexed month from 1 to 12.
3 options
Time zone used to determine the calendar month of an instant string, DateTime, or Date.
BCP 47 locale tag carried on the returned YearMonth and every value derived from it.
falseRead English or localized month names like 'février 2026' and return null on unreadable input with @native-dates/plugins/loose.
Returns
A YearMonth with no day, or null when loose mode cannot read input.
When the loose plugin is loaded, loose mode parses month names in English or the active locale paired with a four-digit year and returns null on unreadable strings:
import { yearMonth } from 'native-dates';
import '@native-dates/plugins/loose';
yearMonth('Nov 2026', { loose: true })?.toString(); // '2026-11'
yearMonth('février 2026', { loose: true, locale: 'fr' })?.toString(); // '2026-02'
yearMonth('not-a-month', { loose: true }); // null