Temporal.Instant.prototype.round()

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

round() 方法是 Temporal.Instant 例項的一個方法,它返回一個新的 Temporal.Instant 物件,該物件表示將此時間戳舍入到指定單位後的結果。

語法

js
round(smallestUnit)
round(options)

引數

smallestUnit(最小單位)

表示 smallestUnit 選項的字串。這是一個便捷的過載,因此 round(smallestUnit) 等價於 round({ smallestUnit }),其中 smallestUnit 是一個字串。

options

一個包含以下部分或全部屬性的物件(按檢索和驗證的順序):

roundingIncrement 可選

一個(截斷為整數的)數字,表示給定 smallestUnit 的舍入增量。預設為 1。增量和 smallestUnit 必須能整除 24 小時;例如,45 秒是 86400 秒的除數,100 分鐘是 3600 分鐘的除數。這比其他類的 round() 方法的要求略微寬鬆,其他類要求增量是該單位最大值的除數。

roundingMode 可選

一個字串,指定如何對 smallestUnit 的小數部分進行四捨五入。參見 Intl.NumberFormat()。預設為 "halfExpand"

smallestUnit(最小單位)

一個字串,表示輸出中包含的最小單位。該值必須是以下之一:"hour""minute""second""millisecond""microsecond""nanosecond",或它們的複數形式。對於大於 "nanosecond" 的單位,smallestUnit 的小數部分將根據 roundingIncrementroundingMode 設定進行舍入。

返回值

一個 Temporal.Instant 新物件,表示將此時間戳舍入到指定單位後的結果,其中所有小於 smallestUnit 的單位都將被歸零。

異常

RangeError

如果任何選項無效,則丟擲。

示例

小單位的四捨五入

js
const instant = Temporal.Instant.fromEpochMilliseconds(1000);
const roundedInstant = instant.round("second");
console.log(roundedInstant.epochMilliseconds); // 1000

const instant2 = instant.round("minute");
console.log(instant2.epochMilliseconds); // 0

規範

規範
Temporal
# sec-temporal.instant.prototype.round

瀏覽器相容性

另見