javascript instance method
Temporal.PlainDateTime.prototype.round()
The round() method of PlainDateTime instances returns a new Temporal.PlainDateTime object representing this date-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.
- : A string representing the
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. For all values ofsmallestUnitexcept"day", 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. For"day", the increment must be 1.
- : A number (truncated to an integer) representing the rounding increment in the given
roundingModeOptional- : A string specifying how to round off the fractional part of
smallestUnit. SeeIntl.NumberFormat(). Defaults to"halfExpand".
- : A string specifying how to round off the fractional part of
smallestUnit- : A string representing the smallest unit to include in the output. The value must be one of the following:
"day","hour","minute","second","millisecond","microsecond","nanosecond", or their plural forms. For units larger than"nanosecond", fractional parts of thesmallestUnitwill be rounded according to theroundingIncrementandroundingModesettings.
- : A string representing the smallest unit to include in the output. The value must be one of the following:
- : An object containing some or all of the following properties (in the order they are retrieved and validated):
Return value
A new PlainDateTime object representing this date-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 dt = Temporal.PlainDateTime.from("2021-07-01T12:34:56.123456789");
const nearestMillisecond = dt.round("millisecond");
console.log(nearestMillisecond.toString()); // 2021-07-01T12:34:56.123
const nearestHalfHour = dt.round({
smallestUnit: "minute",
roundingIncrement: 30,
});
console.log(nearestHalfHour.toString()); // 2021-07-01T12:30:00
const nextDay = dt.round({ smallestUnit: "day", roundingMode: "ceil" });
console.log(nextDay.toString()); // 2021-07-02T00:00:00
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.