Temporal.PlainDateTime.prototype.toString()
Temporal.PlainDateTime 例項的 toString() 方法返回一個字串,表示此日期時間,格式為RFC 9557。
語法
toString()
toString(options)
引數
options可選-
包含以下屬性的物件
calendarName可選-
是否在返回值中顯示日曆註釋 (
[u-ca=calendar_id])。可能的值是:"auto"(預設值)-
如果日曆不是
"iso8601",則包含日曆註釋。 "always"-
始終包含日曆註釋。
"never"-
絕不應包含日曆註釋。這會使得返回的字串無法恢復為相同的
Temporal.PlainDateTime例項,儘管日期值仍然保持不變。 "critical"-
始終包含日曆註釋,並新增一個關鍵標誌:
[!u-ca=calendar_id]。當將字串傳送到某些系統時很有用,但對 Temporal 本身沒有用。
fractionalSecondDigits可選-
一個 0 到 9 之間的整數,或字串
"auto"。預設值為"auto"。如果為"auto",則從小數秒中刪除尾隨零。否則,秒元件的小數部分包含這麼多位數,必要時用零填充或四捨五入。 roundingMode可選-
一個字串,指定如何對超出
fractionalSecondDigits的小數秒位數進行四捨五入。請參閱Intl.NumberFormat()。預設為"trunc"。 smallestUnit可選-
一個字串,指定輸出中包含的最小單位。可能的值是
"minute"、"second"、"millisecond"、"microsecond"和"nanosecond",或它們的複數形式,它們(除了"minute")分別等同於fractionalSecondDigits值為0、3、6、9。如果指定,則忽略fractionalSecondDigits。
返回值
以RFC 9557 格式表示此日期時間的字串。日曆註釋按規定包含在內。
異常
RangeError-
如果任何選項無效,則丟擲。
TypeError-
如果
options不是物件或undefined,則丟擲錯誤。
示例
使用 toString()
const dt = Temporal.PlainDateTime.from("2021-08-01T12:34:56");
console.log(dt.toString()); // '2021-08-01T12:34:56'
有關舍入時間的示例,請參閱 Temporal.PlainTime.prototype.toString()。有關顯示日曆的示例,請參閱 Temporal.PlainDate.prototype.toString()。
規範
| 規範 |
|---|
| Temporal # sec-temporal.plaindatetime.prototype.tostring |
瀏覽器相容性
載入中…