Temporal.Duration.prototype.round()
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The round()
method of Temporal.Duration
instances returns a new Temporal.Duration
object with the duration rounded to the given smallest unit and/or balanced to the given largest unit.
Syntax
round(smallestUnit)
round(options)
Parameters
smallestUnit
-
A string representing the
smallestUnit
option. This is a convenience overload, soround(smallestUnit)
is equivalent toround({ smallestUnit })
, wheresmallestUnit
is a string. options
-
An object containing some or all of the following properties (in the order they are retrieved and validated):
largestUnit
Optional-
Any of the temporal units:
"year"
,"month"
,"week"
,"day"
,"hour"
,"minute"
,"second"
,"millisecond"
,"microsecond"
,"nanosecond"
, or their plural forms, or the value"auto"
which means the largest non-zero component of this duration orsmallestUnit
, whichever is greater. Defaults to"auto"
. The result will not contain units larger than this; for example, if the largest unit is"minute"
, then "1 hour 30 minutes" will become "90 minutes". relativeTo
Optional-
A zoned or plain date(time) that provides the time and calendar information to resolve calendar durations (see the link for the general interpretation of this option). Required if either
this
orother
is a calendar duration, orsmallestUnit
is a calendar unit. roundingIncrement
Optional-
A number (truncated to an integer) representing the rounding increment in the given
smallestUnit
. Defaults to1
. Must be in the inclusive range of 1 to 1e9. If the smallest unit is hours, minutes, seconds, milliseconds, microseconds, or nanoseconds, the increment must be a divisor of the maximum value of the unit; for example, if the unit is hours, the increment must be a divisor of 24 and must not be 24 itself, which means it can be 1, 2, 3, 4, 6, 8, or 12. roundingMode
Optional-
A string representing the rounding mode specifying to round up or down in various scenarios. See
Intl.NumberFormat()
. Defaults to"halfExpand"
. smallestUnit
Optional-
Any of the temporal units:
"year"
,"month"
,"week"
,"day"
,"hour"
,"minute"
,"second"
,"millisecond"
,"microsecond"
,"nanosecond"
, or their plural forms. Defaults to"nanosecond"
. For units larger than"nanosecond"
, fractional parts of thesmallestUnit
will be rounded according to theroundingIncrement
androundingMode
settings. Must be smaller or equal tolargestUnit
. At least one ofsmallestUnit
andlargestUnit
must be provided.
Return value
A new Temporal.Duration
object, where the largest unit is no larger than the largestUnit
option, and the smallest unit is no smaller than the smallestUnit
option. The fractional parts of the smallestUnit
are rounded according to the roundingIncrement
and roundingMode
settings.
Exceptions
RangeError
-
Thrown if any of the options is invalid.
Description
The round()
method performs two operations: rounding and balancing. It does the following:
- It makes sure the duration is balanced. If a component is above its preferred maximum (24 hours per day, 60 minutes per hour, etc.), the excess is carried over to the next larger unit, until we reach
largestUnit
. For example, "24 hours 90 minutes" becomes "25 hours 30 minutes" iflargestUnit
is"auto"
, and "1 day 1 hour 30 minutes" iflargestUnit
is"day"
. - For all components larger than
largestUnit
, they are carried down intolargestUnit
; for example, "2 hours 30 minutes" becomes "150 minutes" iflargestUnit
is"minute"
. - For all components smaller than
smallestUnit
, they are carried up intosmallestUnit
as a fractional part, and then rounded according to theroundingIncrement
androundingMode
settings. For example, "1 hour 30 minutes" becomes "1.5 hours" ifsmallestUnit
is"hour"
, and then rounded to "2 hours" using the default settings.
Calendar units have uneven lengths. Their lengths are resolved relative to a starting point. For example, a duration of "2 years" in the Gregorian calendar may be 730 days or 731 days long, depending on whether it moves through a leap year or not. When rounding to a calendar unit, we first get the exact date-time represented by relativeTo + duration
, then round it down and up according to smallestUnit
and roundingIncrement
to get two candidates. Then, we choose the candidate according to the roundingMode
setting, and finally subtract relativeTo
to get the final duration.
Examples
Rounding off small units
const duration = Temporal.Duration.from({ hours: 1, minutes: 30, seconds: 15 });
const roundedDuration = duration.round("minute");
console.log(roundedDuration.toString()); // "PT1H30M"
Avoiding larger units
const duration = Temporal.Duration.from({
days: 3,
hours: 1,
minutes: 41,
seconds: 5,
});
const roundedDuration = duration.round({ largestUnit: "hour" });
console.log(
`Time spent on this problem: ${roundedDuration.toLocaleString("en-US", { style: "digital" })}`,
);
// Time spent on this problem: 73:41:05
Rounding to a whole number of hours
const duration = Temporal.Duration.from({ days: 1, hours: 1, minutes: 30 });
const roundedDuration = duration.round({
largestUnit: "hour",
smallestUnit: "hour",
roundingMode: "floor",
});
console.log(roundedDuration.hours); // 25
Rounding by 15-minute increments
const duration = Temporal.Duration.from({ hours: 1, minutes: 17 });
const roundedDuration = duration.round({
smallestUnit: "minute",
roundingIncrement: 15,
});
console.log(
`The queue will take approximately ${roundedDuration.toLocaleString("en-US")}`,
);
// The queue will take approximately 1 hr, 15 min
Resolving calendar durations
If either the initial duration or largest/smallest unit contains a calendar unit, you must provide a relativeTo
option to resolve the calendar durations.
const duration = Temporal.Duration.from({ months: 1, days: 1, hours: 1 });
const roundedDuration = duration.round({
largestUnit: "day",
smallestUnit: "day",
relativeTo: Temporal.PlainDateTime.from("2022-01-01"),
});
console.log(roundedDuration); // "P32D"
Specifications
Specification |
---|
Temporal proposal # sec-temporal.duration.prototype.round |
Browser compatibility
BCD tables only load in the browser