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

瀏覽器相容性

另見