Temporal.ZonedDateTime.prototype.until()

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

Temporal.ZonedDateTime 实例的 until() 方法返回一个新的 Temporal.Duration 对象,表示从当前日期时间到另一个日期时间(可以通过 Temporal.ZonedDateTime.from() 转换的形式)的持续时间。如果另一个日期时间在当前日期时间之后,则持续时间为正;如果在之前,则为负。

此方法执行 other - this。要执行 this - other,请使用 since() 方法。

语法

js
until(other)
until(other, options)

参数

其他

一个字符串、一个对象或一个 Temporal.ZonedDateTime 实例,表示要从中减去当前日期时间的日期时间。它使用与 Temporal.ZonedDateTime.from() 相同的算法转换为 Temporal.ZonedDateTime 对象。它必须与 this 具有相同的日历。

options 可选

since() 相同的选项。

返回值

一个新的 Temporal.Duration 对象,表示从当前日期时间“直到”other 的持续时间。如果 other 在当前日期时间之后,则持续时间为正;如果在之前,则为负。

异常

RangeError

在以下情况之一中抛出

  • other 的日历与 this 不同。
  • 任何选项无效。
  • other 的时区与 this 不同,并且 largestUnit"days" 或更大的单位。

示例

使用 until()

js
const flight = Temporal.ZonedDateTime.from(
  "2024-12-21T13:31:00-05:00[America/New_York]",
);
const now = Temporal.Now.zonedDateTimeISO("America/New_York").round("second");
if (Temporal.ZonedDateTime.compare(flight, now) < 0) {
  console.error(
    "The flight is already in the past. The result may not make sense.",
  );
}
const duration = now.until(flight, { largestUnit: "days" });
console.log(`The flight is in ${duration.toLocaleString("en-US")}`);

有关更多示例,请参见 since()

规范

规范
Temporal
# sec-temporal.zoneddatetime.prototype.until

浏览器兼容性

另见