yearMonth()
yearMonth(input, options?): YearMonth
yearMonth(input, zone?): YearMonth
yearMonth(year, month): YearMonthCreates a YearMonth from an ISO year-month 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.
2 options
Time zone used to determine the calendar month of an instant string, DateTime, or Date.
falseRead English month names like 'Nov 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 English month names beside 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('2026 November', { loose: true })?.toString(); // '2026-11'
yearMonth('not-a-month', { loose: true }); // null