Temporal.PlainDateTime.prototype.round()
Temporal.PlainDateTime 例項的 round() 方法返回一個新的 Temporal.PlainDateTime 物件,該物件表示此日期時間舍入到給定單位的結果。
語法
round(smallestUnit)
round(options)
引數
smallestUnit(最小單位)-
表示
smallestUnit選項的字串。這是一個便捷的過載,因此round(smallestUnit)等價於round({ smallestUnit }),其中smallestUnit是一個字串。 options-
一個包含以下部分或全部屬性的物件(按檢索和驗證的順序):
roundingIncrement可選-
一個數字(截斷為整數),表示在給定
smallestUnit下的舍入增量。預設為1。對於除"day"之外的所有smallestUnit值,增量必須是該單位最大值的約數;例如,如果單位是小時,則增量必須是 24 的約數且不能是 24 本身,這意味著它可以是 1、2、3、4、6、8 或 12。對於"day",增量必須是 1。 roundingMode可選-
一個字串,指定如何對
smallestUnit的小數部分進行四捨五入。參見Intl.NumberFormat()。預設為"halfExpand"。 smallestUnit(最小單位)-
一個字串,表示輸出中包含的最小單位。該值必須是以下之一:
"day"、"hour"、"minute"、"second"、"millisecond"、"microsecond"、"nanosecond"或它們的複數形式。對於大於"nanosecond"的單位,smallestUnit的小數部分將根據roundingIncrement和roundingMode設定進行舍入。
返回值
一個新的 Temporal.PlainDateTime 物件,表示此日期時間舍入到給定單位的結果,其中所有小於 smallestUnit 的單位都被清零。
異常
RangeError-
如果任何選項無效,則丟擲。
示例
小單位的四捨五入
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
規範
| 規範 |
|---|
| Temporal # sec-temporal.plaindatetime.prototype.round |
瀏覽器相容性
載入中…