Skip to content

setDate

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

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

  • Wraps Temporal.PlainDate.prototype.with(), which resolves every supplied field in a single atomic overflow pass. This is the safe alternative to composing addDate() calls field-by-field: each sequential .add() resolves overflow against its own intermediate value, so setting month-then-day vs. day-then-month on the same target can silently diverge — .with() has no such order-dependence.
  • fields may set year, month, monthCode, day, 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 date whose day is 31: “constrain” clamps to Feb 29/28, “reject” throws (resulting in “”).
Parameter Type Description
value string ISO PlainDate string
fields Omit&lt;Temporal.PlainDateLike, "calendar"&gt; Partial<Temporal.PlainDateLike> object specifying fields to set

options

Option Type Default
overflow? Overflow

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

setDate("2024-03-10", { year: 2025 }) // "2025-03-10"
setDate("2024-01-31", { month: 2 }) // "2024-02-29" (constrain clamps to the last valid day)
setDate("2024-01-31", { month: 2 }, { overflow: "reject" }) // ""
setDate("2024-03-10", {}) // "2024-03-10" (empty fields object is a no-op)
setDate("invalid", { year: 2025 }) // ""

plain/calculate/setDate.ts