Temporal.PlainDateTime.prototype.eraYear

可用性有限

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

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

Temporal.PlainDateTime 实例的 eraYear 访问器属性会返回一个非负整数,表示此日期在其所属纪年中对应的年份,如果日历不使用纪年(例如 ISO 8601),则返回 undefined。年份索引通常从 1(更常见)或 0 开始,纪年中的年份可能会随时间而递减(例如,公历的公元前)。eraeraYear 结合起来可以唯一地标识日历中的一年,这与 year 的作用方式相同。它 依赖于日历

eraYear 的 set 访问器是 undefined。您不能直接修改此属性。请使用 with() 方法创建一个具有所需新值的 Temporal.PlainDateTime 新实例。

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

示例

使用 eraYear

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

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

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

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

规范

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

浏览器兼容性

另见