setDateTime
Signature
Section titled “Signature”setDateTime(value: string, fields: Omit<PlainDateTimeLike, "calendar" | "era" | "eraYear">, options?: { overflow?: "reject" | "constrain" | undefined; }): stringReturn 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,microsecondand/ornanosecond; 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:
monthCodemust be a string and every other field an integer number.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 an era, 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 PlainDateTime string |
fields | Omit<PlainDateTimeLike, "calendar" | "era" | "eraYear"> | Partial<Temporal.PlainDateTimeLike> 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-time. "constrain" clamps each field to its nearest valid value, so month: 2 on the 31st gives Feb 29 or 28; "reject" returns "". |
Returns
Section titled “Returns”ISO PlainDateTime string with fields set, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { setDateTime } from "@northguild/gmt/plain/calculate";
setDateTime("2024-03-10T12:00:00", { hour: 9 }) // "2024-03-10T09:00:00"