Temporal.PlainDate.prototype.era

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

Temporal.PlainDate 例項的 era 訪問器屬性會返回一個特定日曆、小寫的字串,表示該日期的時代,如果該日曆不使用時代(例如 ISO 8601),則返回 undefinederaeraYear 一起可以唯一地標識日曆中的一個年份,這與 year 的作用相同。它依賴於日曆。對於公曆,它可能是 "gregory""gregory-inverse"

era 的設定訪問器是 undefined。您不能直接更改此屬性。請使用 with() 方法建立一個新的 Temporal.PlainDate 物件,其中包含所需的​​新值。設定時代時,每個程式碼可能有一些別名;例如,"ce""ad" 等同於 "gregory",而 "bce""bc" 等同於 "gregory-inverse"

注意: 此字串不適合向用戶顯示。請使用帶有適當選項的 toLocaleString() 來獲取本地化字串。

示例

使用 era

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

const date2 = Temporal.PlainDate.from("2021-07-01[u-ca=gregory]");
console.log(date2.era); // gregory

const date3 = Temporal.PlainDate.from("-002021-07-01[u-ca=gregory]");
console.log(date3.era); // gregory-inverse

const date4 = Temporal.PlainDate.from("2021-07-01[u-ca=japanese]");
console.log(date4.era); // reiwa

更改時代

您只能為支援時代的日曆設定 era。例如,ISO 8601 日曆沒有時代。請注意,您必須同時提供 eraeraYear

js
const date = Temporal.PlainDate.from("2021-07-01[u-ca=gregory]");
const newDate = date.with({ era: "bc", eraYear: 100 });
console.log(newDate.toString()); // -000099-07-01[u-ca=gregory]

const date2 = Temporal.PlainDate.from("2021-07-01[u-ca=japanese]");
const newDate2 = date2.with({ era: "meiji", eraYear: 1 });
console.log(newDate2.toString()); // 1868-07-01[u-ca=japanese]

規範

規範
Temporal
# sec-get-temporal.plaindate.prototype.era

瀏覽器相容性

另見