roundTime
Signature
Section titled “Signature”roundTime(value: string, options: { smallestUnit: SmallestUnit<TimeUnit>; roundingIncrement?: number; roundingMode?: RoundingMode; }): stringRound an ISO 8601 time string to the specified time unit.
- Returns "" for invalid inputs.
- Wraps Temporal.PlainTime.round() which throws on invalid options.
- Output precision follows
smallestUnit: no fractional seconds for “second” and coarser, 3 digits for “millisecond”, 6 for “microsecond” and 9 for “nanosecond”, so the result never hides the precision the unit asked for.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | string | ISO 8601 time string |
Options
Section titled “Options”options
The unit to round to and how to round
| Option | Type | Default | Description |
|---|---|---|---|
smallestUnit | TimeUnit | PluralUnit<TimeUnit> | Required | The unit to round to, a time unit from "hour" to "nanosecond", singular or plural, as Temporal’s GetTemporalUnitValuedOption accepts both. It also sets how many fractional-second digits the result is written with. A date unit returns "". |
roundingIncrement? | number | 1, Temporal’s default. | How many units make one rounding step. Temporal requires it to divide the next larger unit evenly and be smaller than it (15 minutes, not 7 or 60), and truncates a non-integer; any other value returns "". |
roundingMode? | "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | "halfExpand", Temporal’s default. | Which way a time between two steps goes: one of Temporal’s nine rounding modes, such as "floor", "ceil" or "halfExpand" (to the nearer step, a tie going up). Any other value returns "". |
Returns
Section titled “Returns”Rounded ISO 8601 time string, or "" on invalid input
Examples
Section titled “Examples”import { roundTime } from "@northguild/gmt/plain/calculate";
roundTime("12:34:56", { smallestUnit: "hour" }) // "13:00:00"