Skip to content

setDateTime

setDateTime(value: string, fields: Omit<Temporal.PlainDateTimeLike, "calendar">, options?: { overflow?: Overflow; }): string
import { setDateTime } from "@northguild/gmt/plain/calculate";

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

  • Wraps Temporal.PlainDateTime.prototype.with(), which resolves every supplied field in a single atomic overflow pass — see setDate’s doc for why that matters over composing addDateTime() calls field-by-field.
  • fields may set any of year, month, monthCode, day, hour, minute, second, millisecond, microsecond, nanosecond, era, and/or eraYear; 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 results, e.g. setting month: 2 on a datetime whose day is 31: “constrain” clamps to Feb 29/28, “reject” throws (resulting in “”).
Parameter Type Description
value string ISO PlainDateTime string
fields Omit&lt;Temporal.PlainDateTimeLike, "calendar"&gt; Partial<Temporal.PlainDateTimeLike> object specifying fields to set

options

Option Type Default
overflow? Overflow

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

setDateTime("2024-03-10T12:00:00", { hour: 9 }) // "2024-03-10T09:00:00"
setDateTime("2024-01-31T12:00:00", { month: 2 }) // "2024-02-29T12:00:00" (constrain clamps to the last valid day)
setDateTime("2024-01-31T12:00:00", { month: 2 }, { overflow: "reject" }) // ""
setDateTime("2024-03-10T12:00:00", {}) // "2024-03-10T12:00:00" (empty fields object is a no-op)
setDateTime("invalid", { hour: 9 }) // ""

plain/calculate/setDateTime.ts