Skip to content

roundTime

roundTime(value: string, options: { smallestUnit: SmallestUnit<TimeUnit>; roundingIncrement?: number; roundingMode?: RoundingMode; }): string

Round 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.
ParameterTypeDescription
valuestringISO 8601 time string

options

The unit to round to and how to round

OptionTypeDefaultDescription
smallestUnitTimeUnit | PluralUnit<TimeUnit>RequiredThe 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?number1, 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 "".

Rounded ISO 8601 time string, or "" on invalid input

import { roundTime } from "@northguild/gmt/plain/calculate";
roundTime("12:34:56", { smallestUnit: "hour" }) // "13:00:00"
output 

plain/calculate/roundTime.ts