Temporal.PlainDate.prototype.toString()

可用性有限

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

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

toString() 方法是 Temporal.PlainDate 例項上的一個方法,它返回一個以 RFC 9557 格式表示此日期的字串。

語法

js
toString()
toString(options)

引數

options 可選

包含以下屬性的物件

calendarName 可選

是否在返回值中顯示日曆註釋 ([u-ca=calendar_id])。可能的值是:

"auto"(預設值)

如果日曆不是 "iso8601",則包含日曆註釋。

"always"

始終包含日曆註釋。

"never"

切勿包含日曆註解。這使得返回的字串無法恢復到相同的 Temporal.PlainDate 例項,儘管日期值本身保持不變。

"critical"

始終包含日曆註釋,並新增一個關鍵標誌:[!u-ca=calendar_id]。當將字串傳送到某些系統時很有用,但對 Temporal 本身沒有用。

返回值

一個以 RFC 9557 格式表示此日期的字串。日曆註解將按指定包含在內。

異常

RangeError

如果任何選項無效,則丟擲。

TypeError

如果 options 不是物件或 undefined,則丟擲錯誤。

示例

使用 toString()

js
const date = Temporal.PlainDate.from("2021-08-01");
console.log(date.toString()); // '2021-08-01'

使用選項

js
const isoDate = Temporal.PlainDate.from({ year: 2021, month: 8, day: 1 });
const date = Temporal.PlainDate.from({
  year: 2021,
  month: 8,
  day: 1,
  calendar: "islamic-umalqura",
});
console.log(isoDate.toString({ calendarName: "auto" })); // '2021-08-01'
console.log(date.toString({ calendarName: "auto" })); // '2582-12-17[u-ca=islamic-umalqura]'
console.log(isoDate.toString({ calendarName: "always" })); // '2021-08-01[u-ca=iso8601]'
console.log(date.toString({ calendarName: "always" })); // '2582-12-17[u-ca=islamic-umalqura]'
console.log(date.toString({ calendarName: "never" })); // '2582-12-17'
console.log(isoDate.toString({ calendarName: "critical" })); // '2021-08-01[!u-ca=iso8601]'
console.log(date.toString({ calendarName: "critical" })); // '2582-12-17[!u-ca=islamic-umalqura]'

規範

規範
Temporal
# sec-temporal.plaindate.prototype.tostring

瀏覽器相容性

另見