Skip to content

setTime

setTime(value: string, fields: PlainTimeLike, options?: { overflow?: "reject" | "constrain" | undefined; }): string

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 object with no field set, such as {} or { hour: undefined }, is a no-op.
  • Values are not converted: all six fields must be integer numbers. null, a numeric string, a boolean or a fraction returns "", where Temporal would convert or truncate it. A bigint, NaN or an infinity returns "" too. An out-of-range number follows overflow.
  • An own key that is not one of these fields, such as a misspelt huor, returns "". So does a date field, such as year, or a calendar or timeZone property, whether own, inherited or a getter. A field is read with a property get, as Temporal reads it, so an inherited or getter field is set.
  • A Temporal date or time object returns "", as Temporal’s with refuses one. One whose prototype has been replaced with Object.prototype, null or Function.prototype sets nothing instead.
  • Unlike addTime, which takes no overflow because addition always wraps around the clock, overflow has an effect here: .with() assigns fixed field values rather than adding a delta.
  • Returns "" for invalid input.
ParameterTypeDescription
valuestringISO PlainTime string
fieldsPlainTimeLikePartial<Temporal.PlainTimeLike> object specifying fields to set

options

How an out-of-range field value is handled

OptionTypeDefaultDescription
overflow?"reject" | "constrain" (Overflow)"constrain", Temporal’s default.What to do when a field value is out of range. "constrain" clamps it to the nearest valid value, so hour: 25 becomes 23; "reject" returns "".

ISO PlainTime string with fields set, or "" on invalid input

import { setTime } from "@northguild/gmt/plain/calculate";
setTime("12:00:00", { hour: 9 }) // "09:00:00"
output 

plain/calculate/setTime.ts