Temporal.PlainTime.prototype.round()
Baseline Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
The round() method of Temporal.PlainTime instances returns a new Temporal.PlainTime object representing this time rounded to the given unit.
Syntax
round(smallestUnit) round(options)
Parameters
-
smallestUnit - A string representing the
smallestUnitoption. This is a convenience overload, soround(smallestUnit)is equivalent toround({ smallestUnit }), wheresmallestUnitis a string. -
options - An object containing some or all of the following properties (in the order they are retrieved and validated):
-
roundingIncrementOptional - A number (truncated to an integer) representing the rounding increment in the given
smallestUnit. Defaults to1. The increment must be a divisor of the maximum value ofsmallestUnit; 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. -
roundingModeOptional - A string specifying how to round off the fractional part of
smallestUnit. SeeIntl.NumberFormat(). Defaults to"halfExpand". -
smallestUnit - A string representing the smallest unit to include in the output. The value must be one of the following:
"hour","minute","second","millisecond","microsecond","nanosecond", or their plural forms. For units larger than"nanosecond", fractional parts of thesmallestUnitwill be rounded according to theroundingIncrementandroundingModesettings.
-
Return value
A new Temporal.PlainTime object representing this time rounded to the given unit, where all units smaller than smallestUnit are zeroed out.
Exceptions
-
RangeError - Thrown if any of the options is invalid.
Examples
Rounding off small units
const time = Temporal.PlainTime.from("12:34:56.123456789");
const nearestMillisecond = time.round("millisecond");
console.log(nearestMillisecond.toString()); // 12:34:56.123
const nearestHalfHour = time.round({
smallestUnit: "minute",
roundingIncrement: 30,
});
console.log(nearestHalfHour.toString()); // 12:30:00
const nextHour = time.round({ smallestUnit: "hour", roundingMode: "ceil" });
console.log(nextHour.toString()); // 13:00:00
Specifications
| Specification |
|---|
| Temporal # sec-temporal.plaintime.prototype.round |
Browser compatibility
| 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 | |
round |
144 |
144 |
139 |
128 |
preview |
144 |
139 |
95 |
No |
No |
144 |
No |
1.4.0 |
2.7 |
26.0.0 |
See also
© 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/PlainTime/round