yearMonth()
yearMonth(input, options?): YearMonth
yearMonth(input, zone?): YearMonth
yearMonth(year, month): YearMonth年・月順の文字列、年と1始まりの月の数値、フィールドオブジェクトから、あるいは任意の日付・タイムスタンプの日を除いて YearMonth を作成します。入力が欠けている場合は今月にフォールバックせず、TypeError をスローします。
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'引数
ISO 年月文字列、フィールドオブジェクト、日付またはタイムスタンプ、あるいは西暦の year。
タイムスタンプや loose 文字列を読み取る際のタイムゾーンまたはオプションオブジェクト、あるいは1〜12の1始まりの month。
3件のオプション
瞬間を表す文字列、DateTime、または Date からカレンダーの年月を判定するタイムゾーン。
返される YearMonth とそこから派生するすべての値に引き継がれる BCP 47 ロケールタグ。
false@native-dates/plugins/loose を使用し、'février 2026' のような英語・各言語の月名を読み取り、読み取れない入力には null を返す設定。
戻り値
日を持たない YearMonth。loose モードで input を読み取れない場合は null。
loose プラグインを読み込むと、loose モードでは英語またはアクティブな locale の月名と4桁の年の組み合わせを解析でき、読み取れない文字列には null を返します。
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