native-dates

API / 类

YearMonth class

不含日字段的日历年月,适用于账单周期、银行卡有效期与月份选择器。

import { YearMonth } from 'native-dates'core 14.9 kB min+br

创建 YearMonth

使用 yearMonth() 从 ISO 字符串、年月数字或已有日期创建实例,或使用 monthYear() 按“月/年”顺序解析银行卡有效期字符串。所有实例均不可变,任何修改方法都会返回一个新实例。

create.js
import { monthYear, yearMonth } from 'native-dates';

const billing = yearMonth('2026-11');
const quarterEnd = yearMonth(2026, 12);
const expiry = monthYear('11/26');

你可以通过任意日历日期或具体时刻的 yearMonth 属性读取年月,也可以在链式调用中用 as('yearMonth') 转换:

import { date, datetime, today } from 'native-dates';

date('2026-11-16').yearMonth.toString(); // '2026-11'
datetime('2026-11-01T02:00:00Z', 'America/Los_Angeles').as('yearMonth').toString(); // '2026-10'
const currentMonth = today().yearMonth;

属性

以下属性读取 ISO 年份、月份以及当月的日历边界。

属性 类型 说明
text string 按当前区域格式化的年月,例如 'Nov 2026'。
year、month number 日历年份,以及从 1 开始计数的月份(1 到 12)。
quarter number 日历季度(1 到 4)。
daysInMonth、daysInYear number 当月天数(28 到 31)以及当年天数(365 或 366)。
start、end CalendarDate 当月的第一天和最后一天。
YearMonth.century number monthYear() 解析两位年份时叠加的基准世纪,默认为 2000。

运算与字段

按月、季度或年推算,替换字段,或选取当月的具体某一天。

仅支持月和年单位

由于年月不包含日字段,传入小于月的单位会抛出 RangeError。如需按周或按天运算,请先调用 on() 指定具体日期。

plus()

ym.plus(amount, unit?): YearMonth
ym.add(amount, unit?): YearMonth

加上指定的年、季度或月数,返回新的 YearMonth 实例。同时提供 add() 作为别名。

amountDurationInput必填

与 unit 配对的数值、Duration 实例、时长字符串或字段对象。

unitUnit

当 amount 为数值时的月、季度或年单位名。

YearMonth

加上指定跨度后的新 YearMonth 实例。

const billing = yearMonth('2026-11');

billing.plus(3, 'months').toString(); // '2027-02'
billing.plus('1 year').toString(); // '2027-11'

minus()

ym.minus(amount, unit?): YearMonth
ym.subtract(amount, unit?): YearMonth

减去指定的年、季度或月数,返回新实例。接受与 plus() 相同的参数,同时提供 subtract() 作为别名。

amountDurationInput必填

与 unit 配对的数值、Duration 实例、时长字符串或字段对象。

unitUnit

当 amount 为数值时的月、季度或年单位名。

YearMonth

减去指定跨度后的新 YearMonth 实例。

yearMonth('2026-11').minus(1, 'year').toString(); // '2025-11'

set()

ym.set(fields): YearMonth
ym.set(unit, value): YearMonth

返回一个替换了年份、月份或两者的 YearMonth 新实例。超出范围的月份或小于月的单位都会抛出 RangeError。

fieldsPartial<YearMonthFields> | Unit必填

包含 year 或 month 字段的对象,或单个单位名。

valuenumber

传入单位名时对应的整数值。

YearMonth

更新日历字段后的新 YearMonth 实例。

const billing = yearMonth('2026-11');

billing.set('month', 2).toString(); // '2026-02'
billing.set({ year: 2028, month: 2 }).daysInMonth; // 29

on()

ym.on(day): CalendarDate

在当前月份中指定 1 到 31 之间的整数日期,返回一个 CalendarDate。在 2 月、4 月等天数较少的月份中,31 日会自动限制到月末最后一天;超出 1 到 31 范围的数值则会抛出 RangeError。

daynumber | string必填

当月的日期数字(1 到 31 之间的整数)。

CalendarDate

当月对应那一天的 CalendarDate 实例。

yearMonth('2026-11').on(15).toString(); // '2026-11-15'
yearMonth('2026-02').on(31).toString(); // '2026-02-28'
yearMonth('2028-02').on(31).toString(); // '2028-02-29'

比较与包含

按时间顺序比较年月,或判断某个日历日期是否落在该月份内。使用 < 等关系运算符会抛出错误;对数组排序时,请使用 compare() 和边界辅助函数。

compare()

ym.compare(other): -1 | 0 | 1

将当前年月与另一个年月比较先后,返回负数、零或正数以便排序。传入完整日期或时间戳时,会与其所属月份比较。

otherYearMonthInput必填

另一个 YearMonth 实例,或任意 yearMonth() 接受的输入。

-1 | 0 | 1

更早时返回 -1,相等时返回 0,更晚时返回 1。

yearMonth('2026-05').compare('2026-11'); // -1
yearMonth('2026-11').compare('2026-11-16'); // 0

equals()

ym.equals(other): boolean

当两个值表示相同的年份和月份时返回 true。

yearMonth('2026-11').equals(yearMonth(2026, 11)); // true

isBefore()

ym.isBefore(other): boolean

当前年月早于另一个年月时返回 true。

yearMonth('2026-10').isBefore('2026-11'); // true

isAfter()

ym.isAfter(other): boolean

当前年月晚于另一个年月时返回 true。

yearMonth('2026-12').isAfter('2026-11'); // true

contains()

ym.contains(day): boolean
ym.includes(day): boolean

当给定的日历日期或时间戳落在该年该月内时返回 true。同时提供 includes() 作为别名。

dayCalendarDateInput必填

日期字符串、CalendarDate、DateTime、字段对象或 Date。

boolean

当 day 落在该月份内时返回 true。

const nov = yearMonth('2026-11');

nov.contains('2026-11-16'); // true
nov.contains('2026-12-01'); // false

isPast()

ym.isPast(zone?): boolean

在指定或全局配置的时区下,当整个月份都早于今天所在月份时返回 true。

yearMonth('2020-01').isPast('UTC'); // true

isFuture()

ym.isFuture(zone?): boolean

在指定或全局配置的时区下,当该月份晚于今天所在月份时返回 true。

yearMonth('2099-01').isFuture('UTC'); // true

isLeapYear()

ym.isLeapYear(): boolean

当该月份所属的日历年份有 366 天时返回 true。

yearMonth('2028-02').isLeapYear(); // true
yearMonth('2026-02').isLeapYear(); // false

度量跨度

until()

ym.until(other): Duration
ym.until(other, unit): number

度量从当前月份到目标月份之间的跨度;若目标更早,则返回负时长。返回的 Duration 会记住当前月份的起点,因此其中的月和年可按任意单位求总计。

otherYearMonthInput必填

作为终点的目标年月。

unitUnit

单位名,指定后将以浮点数返回换算后的总跨度。

Duration | number

包含年与月的 Duration 实例;若传入 unit 则返回数值。

const start = yearMonth('2026-03');

start.until('2027-06').toString(); // 'P1Y3M'
start.until('2027-06', 'months'); // 15

since()

ym.since(other): Duration
ym.since(other, unit): number

度量自较早年月以来经过的跨度。传入单位名可直接以数值形式返回总跨度。

yearMonth('2026-11').since('2026-05', 'months'); // 6

格式化与转换

format()

ym.format(spec?, locale?): string

使用命名预设、Intl 选项对象或内置占位符模板(如 'MM/YY')来格式化年月。传入共用选项对象时,会自动丢弃日、时钟和时区相关的键。

标准预设与本地化预设都只输出年份和月份,通过 configure() 注册的自定义格式同样适用。完整的预设与占位符列表请参阅格式化。

specFormatSpec默认 'medium'

预设名称(如 'medium' 或 'long')、Intl 选项对象或占位符模板。

localeLocale

BCP 47 区域标签,仅对本次调用覆盖实例自带或全局配置的区域设置。

string

格式化后的年月字符串。

const nov = yearMonth('2026-11');

nov.format('short', 'en-US'); // '11/2026'
nov.format('medium', 'en-US'); // 'Nov 2026'
nov.format('long', 'en-US'); // 'November 2026'
nov.format('L', 'en-US'); // '11/2026'
nov.format('MM/YY'); // '11/26'

locale()

ym.locale(): Locale | undefined
ym.locale(tag): YearMonth

读取当前年月自带的区域设置,或返回绑定了新区域的副本,该区域会作用于文本预览、预设格式及所有由此派生的值。传入空标签可清除已绑定的区域;同时提供 lang() 作为别名。

tagLocale | null | undefined

要绑定并随派生值传递的 BCP 47 区域标签;传入 null 可清除已有标签。

YearMonth | Locale | undefined

不传参数时返回当前生效的区域标签;传入参数时返回携带 tag 的新 YearMonth 实例。

const may = yearMonth('2026-05').locale('ru');

may.locale(); // 'ru'
may.format('long'); // 'май 2026 г.'
may.on(1).format('long'); // '1 мая 2026 г.'

lang()

ym.lang(): Locale | undefined
ym.lang(tag): YearMonth

locale() 的简写别名,用于读取或绑定当前年月的 BCP 47 区域标签。

as()

ym.as('yearMonth'): YearMonth

在通用转换链中请求 'yearMonth' 时返回当前实例,请求其他类型时抛出 RangeError。

toString()

ym.toString(): string
ym.toJSON(): string

返回 ISO 8601 年月字符串,与 HTML 中的 month 表单输入格式一致。toJSON() 返回相同的字符串,供 JSON 序列化使用。

string

ISO 8601 年月字符串,例如 '2026-11'。

yearMonth(2026, 11).toString(); // '2026-11'
JSON.stringify({ billing: yearMonth(2026, 11) }); // '{"billing":"2026-11"}'

toTemporal()

ym.toTemporal(): Temporal.PlainYearMonth

返回底层的 Temporal.PlainYearMonth 实例。

valueOf()

ym.valueOf(): never

始终抛出 RangeError,防止数值运算符将年月隐式转为数字。如需收窄未知值的类型,请使用 isYearMonth() 或 kindOf()。

编辑此页更新于 2026年11月14日