大网数据广告活动
Web 标准与前端开发

Temporal.PlainDateTime.prototype.toString()

· 52CE 我爱测

The **`toString()`** method of Temporal.PlainDateTime instances returns a string representing this date-time in the RFC 9557 format.

The toString() method of Temporal.PlainDateTime instances returns a string representing this date-time in the RFC 9557 format.

Syntax

toString()
toString(options)

Parameters

  • options
    • : An object containing the following property:
      • calendarName
        • : Whether to show the calendar annotation ([u-ca=calendar_id]) in the return value. Possible values are:
          • "auto" (default)
            • : Include the calendar annotation if the calendar is not "iso8601".
          • "always"
            • : Always include the calendar annotation.
          • "never"
            • : Never include the calendar annotation. This makes the returned string not recoverable to the same Temporal.PlainDateTime instance, although the date value still remains the same.
          • "critical"
            • : Always include the calendar annotation, and add a critical flag: [!u-ca=calendar_id]. Useful when sending the string to certain systems, but not useful for Temporal itself.
      • fractionalSecondDigits
        • : Either an integer from 0 to 9, or the string "auto". The default is "auto". If "auto", then trailing zeros are removed from the fractional seconds. Otherwise, the fractional part of the second component contains this many digits, padded with zeros or rounded as necessary.
      • roundingMode
        • : A string specifying how to round off fractional second digits beyond fractionalSecondDigits. See Intl.NumberFormat(). Defaults to "trunc".
      • smallestUnit
        • : A string specifying the smallest unit to include in the output. Possible values are "minute", "second", "millisecond", "microsecond", and "nanosecond", or their plural forms, which (except "minute") are equivalent to fractionalSecondDigits values of 0, 3, 6, 9, respectively. If specified, then fractionalSecondDigits is ignored.

Return value

A string in the RFC 9557 format representing this date-time. The calendar annotation is included as specified.

Exceptions

  • RangeError
    • : Thrown if any of the options is invalid.
  • TypeError
    • : Thrown if options is not an object or undefined.

Examples

Using toString()

const dt = Temporal.PlainDateTime.from("2021-08-01T12:34:56");
console.log(dt.toString()); // '2021-08-01T12:34:56'

For examples with rounding times, see Temporal/PlainTime/toString. For examples with displaying calendars, see Temporal/PlainDate/toString.

Specifications

Browser compatibility

See also

  • Temporal.PlainDateTime
  • Temporal/PlainDateTime/from
  • Temporal/PlainDateTime/toJSON
  • Temporal/PlainDateTime/toLocaleString

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。