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

瀏覽器相容性

另見