Temporal.PlainYearMonth.prototype.eraYear

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

Temporal.PlainYearMonth 实例的 **eraYear** 访问器属性返回一个非负整数,表示此年月在其纪年中对应的年份,如果日历不使用纪年(例如 ISO 8601),则返回 undefined。年份索引通常从 1(更常见)或 0 开始,并且纪年中的年份可能随时间减小(例如,公历的公元前)。eraeraYear 一起可以唯一地标识日历中的一年,就像 year 一样。它取决于 日历

eraYear 的设置访问器是 undefined。您不能直接更改此属性。请使用 with() 方法创建一个具有所需新值的 Temporal.PlainYearMonth 新对象。

有关一般信息和更多示例,请参阅 Temporal.PlainDate.prototype.eraYear

示例

使用 eraYear

js
const ym = Temporal.PlainYearMonth.from("2021-07"); // ISO 8601 calendar
console.log(ym.eraYear); // undefined

const ym2 = Temporal.PlainYearMonth.from("2021-07-01[u-ca=gregory]");
console.log(ym2.eraYear); // 2021

const ym3 = Temporal.PlainYearMonth.from("-002021-07-01[u-ca=gregory]");
console.log(ym3.eraYear); // 2022; 0000 is used for the year 1 BC

const ym4 = Temporal.PlainYearMonth.from("2021-07-01[u-ca=japanese]");
console.log(ym4.eraYear); // 3

规范

规范
Temporal
# sec-get-temporal.plainyearmonth.prototype.erayear

浏览器兼容性

另见