Temporal.Duration.prototype.negated()

可用性有限

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

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

Temporal.Duration 实例的 negated() 方法返回一个新的 Temporal.Duration 对象,该对象的负值是此持续时间(所有字段保持相同的绝对值,但符号反转)。

语法

js
negated()

参数

无。

返回值

一个新的 Temporal.Duration 对象,其中所有字段的绝对值与此持续时间相同,但符号反转(正字段变为负,反之亦然)。

示例

使用 negated()

js
const d1 = Temporal.Duration.from({ hours: 1, minutes: 30 });
const d2 = Temporal.Duration.from({ hours: -1, minutes: -30 });

console.log(d1.negated().toString()); // "-PT1H30M"
console.log(d2.negated().toString()); // "PT1H30M"

规范

规范
Temporal
# sec-temporal.duration.prototype.negated

浏览器兼容性

另见