Skip to content

setDate

setDate(value: string, fields: Omit<PlainDateLike, "calendar" | "era" | "eraYear">, options?: { overflow?: "reject" | "constrain" | undefined; }): string

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 and/or day; omitted fields keep their current value. An object with no field set, such as {} or { year: undefined }, is a no-op.
  • Values are not converted: year, month and day must be integer numbers and monthCode a string. 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. A number too large for its field follows overflow. A month or day below 1, or a date outside Temporal’s range, returns "".
  • An own key that is not one of these fields, such as a misspelt yaer, returns "". So does a field this function does not take, such as hour, 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.
  • Returns "" for invalid input.
ParameterTypeDescription
valuestringISO PlainDate string
fieldsOmit<PlainDateLike, "calendar" | "era" | "eraYear">Partial<Temporal.PlainDateLike> 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 the fields do not form a real date. "constrain" clamps to the last valid day, so month: 2 on the 31st gives Feb 29 or 28; "reject" returns "".

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

import { setDate } from "@northguild/gmt/plain/calculate";
setDate("2024-03-10", { year: 2025 }) // "2025-03-10"
output 

plain/calculate/setDate.ts