Temporal.PlainDate.prototype.year

可用性有限

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

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

Temporal.PlainDate 实例的 year 访问器属性返回一个整数,表示此日期相对于特定日历的纪元年份起点的年数。它依赖于日历

此属性的功能与 era/eraYear 对功能相同,它们共同唯一标识日历中的年份。通常,年份 1 是最新纪元的第一年,或者是 ISO 8601 年份 0001。由于 year 是相对于纪元年份的起点,而不是纪元日期的起点,因此如果纪元位于年中,则该年份在纪元开始日期之前和之后的值将相同。

year 的 set 访问器为 undefined。您无法直接更改此属性。请使用 with() 方法创建一个具有所需新值的 Temporal.PlainDate 新对象。

示例

使用 year

js
const date = Temporal.PlainDate.from("2021-07-01"); // ISO 8601 calendar
console.log(date.year); // 2021

const date2 = Temporal.PlainDate.from("-002021-07-01");
console.log(date2.year); // -2021

const date3 = Temporal.PlainDate.from("2021-07-01[u-ca=japanese]");
console.log(date3.year); // 2021; although the Japanese calendar uses eras,
// there's no obvious "default era", so the year is the same as the ISO year

const date4 = Temporal.PlainDate.from("2021-07-01[u-ca=hebrew]");
console.log(date4.year); // 5781; the Hebrew calendar uses the Anno Mundi epoch, which starts in 3761 BC

规范

规范
Temporal
# sec-get-temporal.plaindate.prototype.year

浏览器兼容性

另见