Temporal.PlainDate.from()
Temporal.PlainDate.from() 靜態方法從另一個 Temporal.PlainDate 物件、具有日期屬性的物件或 RFC 9557 字串建立一個新的 Temporal.PlainDate 物件。
語法
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選項如何,都必須是正數。 era和eraYear-
一個字串和一個整數,它們與
era和eraYear屬性對應。僅當日歷系統具有紀元時才使用。era和eraYear必須同時提供。必須提供eraYear(與era一起)或year中的至少一個。如果同時提供了era、eraYear和year,則它們必須一致。 月份-
與
month屬性對應。無論overflow選項如何,都必須是正數。必須提供month或monthCode中的至少一個。如果同時提供了month和monthCode,則它們必須一致。 monthCode-
與
monthCode屬性對應。必須提供month或monthCode中的至少一個。如果同時提供了month和monthCode,則它們必須一致。 年-
與
year屬性對應。必須提供eraYear(與era一起)或year中的至少一個。如果同時提供了era、eraYear和year,則它們必須一致。
資訊應明確指定年份(作為
year或era和eraYear)、月份(作為month或monthCode)和日期。
-
options可選-
包含以下屬性的物件
overflow可選-
一個字串,指定當日期元件超出範圍時(使用物件
info時)的行為。可能的值有:"constrain"(預設)-
日期元件被限制在有效範圍內。
"reject"-
如果日期元件超出範圍,則丟擲
RangeError。
返回值
一個新的 Temporal.PlainDate 物件,表示 info 在指定 calendar 中指定的日期。
異常
TypeError-
在以下情況之一中丟擲
info不是物件或字串。options不是物件或undefined。- 提供的屬性不足以明確確定日期。您通常需要提供
year(或era和eraYear)、month(或monthCode)和day。
RangeError-
在以下情況之一中丟擲
- 指定相同元件的提供的屬性不一致。
- 提供的非數字屬性無效;例如,如果
monthCode在此日曆中從未是有效的月份程式碼。 - 提供的數字屬性超出範圍,並且
options.overflow設定為"reject"。 - 該資訊不在 可表示範圍 內,該範圍是距 Unix 紀元 ±(108 + 1) 天,或大約 ±273,972.6 年。
示例
從物件建立 PlainDate
// 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]"
控制溢位行為
預設情況下,超出範圍的值會被鉗制到有效範圍
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"
你可以將此行為更改為丟擲錯誤
const d3 = Temporal.PlainDate.from(
{ year: 2021, month: 13, day: 1 },
{ overflow: "reject" },
);
// RangeError: date value "month" not in 1..12: 13
從字串建立 PlainDate
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
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 |
瀏覽器相容性
載入中…