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 |
瀏覽器相容性
載入中…