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 |
瀏覽器相容性
載入中…
另見
Temporal.PlainDateTemporal.PlainDate.prototype.with()Temporal.PlainDate.prototype.add()Temporal.PlainDate.prototype.subtract()Temporal.PlainDate.prototype.eraTemporal.PlainDate.prototype.eraYearTemporal.PlainDate.prototype.yearOfWeekTemporal.PlainDate.prototype.monthTemporal.PlainDate.prototype.day