Temporal.PlainDate.prototype.era
Temporal.PlainDate 实例的 era 访问器属性会返回一个特定日历、小写的字符串,表示该日期的时代,如果该日历不使用时代(例如 ISO 8601),则返回 undefined。era 和 eraYear 一起可以唯一地标识日历中的一个年份,这与 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 日历没有时代。请注意,您必须同时提供 era 和 eraYear。
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 |
浏览器兼容性
加载中…