Temporal.PlainDate.from()

可用性有限

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

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

Temporal.PlainDate.from() 靜態方法從另一個 Temporal.PlainDate 物件、具有日期屬性的物件或 RFC 9557 字串建立一個新的 Temporal.PlainDate 物件。

語法

js
Temporal.PlainDate.from(info)
Temporal.PlainDate.from(info, options)

引數

info

以下之一:

  • 一個 Temporal.PlainDate 例項,它會建立該例項的一個副本。

  • 一個 Temporal.PlainDateTime 例項,它以與 Temporal.PlainDateTime.prototype.toPlainDate() 相同的方式提供日曆日期。

  • 一個 Temporal.ZonedDateTime 例項,它以與 Temporal.ZonedDateTime.prototype.toPlainDate() 相同的方式提供日曆日期。

  • 一個包含日期和可選日曆的 RFC 9557 字串。

  • 一個包含以下屬性的物件(按檢索和驗證的順序)

    calendar 可選

    一個與 calendarId 屬性對應的字串。有關常用支援的日曆型別列表,請參閱 Intl.supportedValuesOf()。預設為 "iso8601"。所有其他屬性都以該日曆系統解釋(與 Temporal.PlainDate() 建構函式不同,後者在 ISO 日曆系統中解釋這些值)。

    一個與 day 屬性對應的整數。無論 overflow 選項如何,都必須是正數。

    eraeraYear

    一個字串和一個整數,它們與 eraeraYear 屬性對應。僅當日歷系統具有紀元時才使用。eraeraYear 必須同時提供。必須提供 eraYear(與 era 一起)或 year 中的至少一個。如果同時提供了 eraeraYearyear,則它們必須一致。

    月份

    month 屬性對應。無論 overflow 選項如何,都必須是正數。必須提供 monthmonthCode 中的至少一個。如果同時提供了 monthmonthCode,則它們必須一致。

    monthCode

    monthCode 屬性對應。必須提供 monthmonthCode 中的至少一個。如果同時提供了 monthmonthCode,則它們必須一致。

    year 屬性對應。必須提供 eraYear(與 era 一起)或 year 中的至少一個。如果同時提供了 eraeraYearyear,則它們必須一致。

    資訊應明確指定年份(作為 yeareraeraYear)、月份(作為 monthmonthCode)和日期。

options 可選

包含以下屬性的物件

overflow 可選

一個字串,指定當日期元件超出範圍時(使用物件 info 時)的行為。可能的值有:

"constrain"(預設)

日期元件被限制在有效範圍內。

"reject"

如果日期元件超出範圍,則丟擲 RangeError

返回值

一個新的 Temporal.PlainDate 物件,表示 info 在指定 calendar 中指定的日期。

異常

TypeError

在以下情況之一中丟擲

  • info 不是物件或字串。
  • options 不是物件或 undefined
  • 提供的屬性不足以明確確定日期。您通常需要提供 year(或 eraeraYear)、month(或 monthCode)和 day
RangeError

在以下情況之一中丟擲

  • 指定相同元件的提供的屬性不一致。
  • 提供的非數字屬性無效;例如,如果 monthCode 在此日曆中從未是有效的月份程式碼。
  • 提供的數字屬性超出範圍,並且 options.overflow 設定為 "reject"
  • 該資訊不在 可表示範圍 內,該範圍是距 Unix 紀元 ±(108 + 1) 天,或大約 ±273,972.6 年。

示例

從物件建立 PlainDate

js
// Year, month, and day
const d1 = Temporal.PlainDate.from({ year: 2021, month: 7, day: 1 });
console.log(d1.toString()); // "2021-07-01"

// Year, month code, and day
const d2 = Temporal.PlainDate.from({ year: 2021, monthCode: "M07", day: 1 });
console.log(d2.toString()); // "2021-07-01"

// Year, month, day in a different calendar
const d3 = Temporal.PlainDate.from({
  year: 2021,
  month: 7,
  day: 1,
  calendar: "chinese",
});
// Note: when you construct a date with an object, the date components
// are in *that* calendar, not the ISO calendar. However, toString() always
// outputs the date in the ISO calendar. For example, the year "2021" in
// the Chinese calendar is actually 616 BC in the ISO calendar.
console.log(d3.toString()); // "-000616-08-12[u-ca=chinese]"

// Era, eraYear, month, and day
const d4 = Temporal.PlainDate.from({
  era: "meiji",
  eraYear: 4,
  month: 7,
  day: 1,
  calendar: "japanese",
});
console.log(d4.toString()); // "1871-07-01[u-ca=japanese]"

控制溢位行為

預設情況下,超出範圍的值會被鉗制到有效範圍

js
const d1 = Temporal.PlainDate.from({ year: 2021, month: 13, day: 1 });
console.log(d1.toString()); // "2021-12-01"

const d2 = Temporal.PlainDate.from({ year: 2021, month: 2, day: 29 });
console.log(d2.toString()); // "2021-02-28"

const d3 = Temporal.PlainDate.from("2021-02-29");
console.log(d3.toString()); // "2021-02-28"

你可以將此行為更改為丟擲錯誤

js
const d3 = Temporal.PlainDate.from(
  { year: 2021, month: 13, day: 1 },
  { overflow: "reject" },
);
// RangeError: date value "month" not in 1..12: 13

從字串建立 PlainDate

js
const d = Temporal.PlainDate.from("2021-07-01");
console.log(d.toLocaleString("en-US", { dateStyle: "full" }));
// Thursday, July 1, 2021

// Providing a calendar
const d2 = Temporal.PlainDate.from("2021-07-01[u-ca=japanese]");
console.log(
  d2.toLocaleString("ja-JP", { calendar: "japanese", dateStyle: "full" }),
);
// 令和3年7月1日木曜日

// Providing a time and an offset (ignored)
const d3 = Temporal.PlainDate.from("2021-07-01T00:00+08:00");
console.log(d3.toString()); // "2021-07-01"

從另一個 Temporal 例項建立 PlainDate

js
const dt = Temporal.PlainDateTime.from("2021-07-01T12:00");
const d = Temporal.PlainDate.from(dt);
console.log(d.toString()); // "2021-07-01"

const zdt = Temporal.ZonedDateTime.from(
  "2021-07-01T00:00+08:00[Asia/Shanghai]",
);
const d2 = Temporal.PlainDate.from(zdt);
console.log(d2.toString()); // "2021-07-01"

const d3 = Temporal.PlainDate.from(d);
console.log(d3.toString()); // "2021-07-01"

規範

規範
Temporal
# sec-temporal.plaindate.from

瀏覽器相容性

另見