setDateTime
Signature
Section titled “Signature”setDateTime(value: string, fields: Omit<Temporal.PlainDateTimeLike, "calendar">, options?: { overflow?: Overflow; }): stringimport { 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 — seesetDate’s doc for why that matters over composingaddDateTime()calls field-by-field. fieldsmay set any ofyear,month,monthCode,day,hour,minute,second,millisecond,microsecond,nanosecond,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 datetime whosedayis 31: “constrain” clamps to Feb 29/28, “reject” throws (resulting in “”).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
ISO PlainDateTime string |
fields |
Omit<Temporal.PlainDateTimeLike, "calendar"> |
Partial<Temporal.PlainDateTimeLike> object specifying fields to set |
Options
Section titled “Options”options
| Option | Type | Default |
|---|---|---|
overflow? |
Overflow |
— |
Returns
Section titled “Returns”ISO PlainDateTime string with fields set, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”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 }) // ""