Skip to content

setTime

setTime(value: string, fields: Temporal.PlainTimeLike, options?: { overflow?: Overflow; }): string
import { setTime } from "@northguild/gmt/plain/calculate";

Return a PlainTime ISO string with the given fields set on value.

  • Wraps Temporal.PlainTime.prototype.with(), which resolves every supplied field in a single atomic overflow pass — see setDate’s doc for why that matters over composing addTime() calls field-by-field.
  • fields may set any of hour, minute, second, millisecond, microsecond, and/or nanosecond; omitted fields keep their current value. An empty object is a no-op.
  • Returns “” for invalid input. overflow (“constrain” (default) | “reject”) controls out-of-range field values, e.g. hour: 25: “constrain” clamps to 23, “reject” throws (resulting in “”). Unlike addTime (where overflow is inert because addition always wraps around the clock), overflow has a real effect here because .with() assigns fixed field values rather than adding a delta.
Parameter Type Description
value string ISO PlainTime string
fields Temporal.PlainTimeLike Partial<Temporal.PlainTimeLike> object specifying fields to set

options

Option Type Default
overflow? Overflow

ISO PlainTime string with fields set, or “” on invalid input

setTime("12:00:00", { hour: 9 }) // "09:00:00"
setTime("12:00:00", { hour: 25 }) // "23:00:00" (constrain clamps to the max valid hour)
setTime("12:00:00", { hour: 25 }, { overflow: "reject" }) // ""
setTime("12:00:00", {}) // "12:00:00" (empty fields object is a no-op)
setTime("invalid", { hour: 9 }) // ""

plain/calculate/setTime.ts