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

Temporal.PlainDateTime.from()

· 52CE 我爱测

The **`Temporal.PlainDateTime.from()`** static method creates a new `Temporal.PlainDateTime` object from another `Temporal.PlainDateTime` object, an object with date and time properties, or an RFC 9557 string.

The Temporal.PlainDateTime.from() static method creates a new Temporal.PlainDateTime object from another Temporal.PlainDateTime object, an object with date and time properties, or an RFC 9557 string.

Syntax

Temporal.PlainDateTime.from(info)
Temporal.PlainDateTime.from(info, options)

Parameters

  • info
    • : One of the following:
      • A Temporal.PlainDateTime instance, which creates a copy of the instance.
      • An RFC 9557 string containing a date, optionally a time, and optionally a calendar.
      • An object containing properties that are recognized by either Temporal/PlainDate/from (calendar, era, eraYear, year, month, monthCode, day) or Temporal/PlainTime/from (hour, minute, second, millisecond, microsecond, nanosecond). The info should explicitly specify a year (as year or era and eraYear), a month (as month or monthCode), and a day; others are optional and will be set to their default values.
  • options
    • : An object containing the following property:
      • overflow
        • : A string specifying the behavior when a date component is out of range (when using the object info). Possible values are:
          • "constrain" (default)
            • : The date component is clamped to the valid range.
          • "reject"
            • : A RangeError is thrown if the date component is out of range.

Return value

A new Temporal.PlainDateTime object, representing the date and time specified by info in the specified calendar.

Exceptions

  • TypeError
    • : Thrown in one of the following cases:
      • info is not an object or a string.
      • options is not an object or undefined.
      • The provided properties are insufficient to unambiguously determine a date. You usually need to provide a year (or era and eraYear), a month (or monthCode), and a day.
  • RangeError
    • : Thrown in one of the following cases:
      • The provided properties that specify the same component are inconsistent.
      • The provided non-numerical properties are not valid; for example, if monthCode is never a valid month code in this calendar.
      • The provided numerical properties are out of range, and options.overflow is set to "reject".
      • The info is not in the representable range, which is ±(10<sup>8</sup> + 1) days, or about ±273,972.6 years, from the Unix epoch.

Examples

Creating a PlainDateTime from an object

// Year + month + day + hour + minute + second
const dt = Temporal.PlainDateTime.from({
  year: 2021,
  month: 7,
  day: 1,
  hour: 12,
  minute: 34,
  second: 56,
});
console.log(dt.toString()); // "2021-07-01T12:34:56"

Creating a PlainDateTime from a string

const dt = Temporal.PlainDateTime.from("2021-07-01T12:34:56");
console.log(dt.toLocaleString()); // "7/1/2021, 12:34:56 PM" (assuming en-US locale)

For more examples, especially regarding different calendars and overflow settings, see Temporal/PlainDate/from and Temporal/PlainTime/from.

Specifications

Browser compatibility

See also

  • Temporal.PlainDateTime
  • Temporal/PlainDateTime/PlainDateTime
  • Temporal/PlainDateTime/with

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

资料来源与适用范围

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

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

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