setDate
Signature
Section titled “Signature”setDate(value: string, fields: Omit<Temporal.PlainDateLike, "calendar">, options?: { overflow?: Overflow; }): stringimport { 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 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,monthCode,day,era, and/oreraYear; 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. settingmonth: 2on a date whosedayis 31: “constrain” clamps to Feb 29/28, “reject” throws (resulting in “”).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
ISO PlainDate string |
fields |
Omit<Temporal.PlainDateLike, "calendar"> |
Partial<Temporal.PlainDateLike> object specifying fields to set |
Options
Section titled “Options”options
| Option | Type | Default |
|---|---|---|
overflow? |
Overflow |
— |
Returns
Section titled “Returns”ISO PlainDate string with fields set, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”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 }) // ""