setDate
Signature
Section titled “Signature”setDate(value: string, fields: Omit<PlainDateLike, "calendar" | "era" | "eraYear">, options?: { overflow?: "reject" | "constrain" | undefined; }): stringReturn 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 composingaddDate()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. fieldsmay setyear,month,monthCodeand/orday; 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,monthanddaymust be integer numbers andmonthCodea string.null, a numeric string, a boolean or a fraction returns "", where Temporal would convert or truncate it. A bigint,NaNor an infinity returns "" too. A number too large for its field followsoverflow. 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 ashour, or acalendarortimeZoneproperty, 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
withrefuses one. One whose prototype has been replaced withObject.prototype,nullorFunction.prototypesets nothing instead. - Returns "" for invalid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | string | ISO PlainDate string |
fields | Omit<PlainDateLike, "calendar" | "era" | "eraYear"> | Partial<Temporal.PlainDateLike> object specifying fields to set |
Options
Section titled “Options”options
How an out-of-range field value is handled
| Option | Type | Default | Description |
|---|---|---|---|
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 "". |
Returns
Section titled “Returns”ISO PlainDate string with fields set, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { setDate } from "@northguild/gmt/plain/calculate";
setDate("2024-03-10", { year: 2025 }) // "2025-03-10"