This feature is not Baseline because it does not work in some of the most widely-used browsers.
The with() method of Temporal.Duration instances returns a new Temporal.Duration object representing this duration with some fields replaced by new values. Because all Temporal objects are designed to be immutable, this method essentially functions as the setter for the duration's fields.
with(info)
infoAn object containing at least one of the properties recognized by Temporal.Duration.from(): years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds. Unspecified properties use the values from the original duration.
A new Temporal.Duration object, where the fields specified in info that are not undefined are replaced by the corresponding values, and the rest of the fields are copied from the original duration.
RangeErrorThrown in one of the following cases:
info object is not an integer (including non-finite values).TypeErrorThrown in one of the following cases:
info object is not an object.info object are undefined.You can use with() to achieve fine-grained control over the fields of a Temporal.Duration object. For example, you can manually balance a duration only on one unit, which round() does not offer:
function balanceMinutes(duration) {
const { hours, minutes } = duration;
const totalMinutes = hours * 60 + minutes;
const balancedMinutes = totalMinutes % 60;
const balancedHours = (totalMinutes - balancedMinutes) / 60;
return duration.with({ hours: balancedHours, minutes: balancedMinutes });
}
const d1 = Temporal.Duration.from({ hours: 100, minutes: 100, seconds: 100 });
const d2 = balanceMinutes(d1);
console.log(d2.hours); // 101
console.log(d2.minutes); // 40
console.log(d2.seconds); // 100; remains unbalanced
| Specification |
|---|
| Temporal> # sec-temporal.duration.prototype.with> |
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
with |
144 | 144 | 139 | No | preview | 144 | 139 | No | No | No | 144 | No | ? | 1.40 | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/with