setZoned
Signature
Section titled “Signature”setZoned(value: string, fields: Omit<Temporal.ZonedDateTimeLike, "calendar" | "timeZone" | "offset">, options?: { overflow?: Overflow; disambiguation?: Disambiguation; offset?: Offset; }): stringimport { setZoned } from "@northguild/gmt/zoned/calculate";Return a zoned ISO 8601 datetime string with the given fields set on value.
- Wraps
Temporal.ZonedDateTime.prototype.with(), which resolves every supplied field in a single atomic overflow pass. This is the safe alternative to composingaddZoned()calls field-by-field, and it is the only construction path that can reproducestartOfZoned’s disambiguation-plus-offset handling —addZoned()has nooffsetcontrol equivalent to.with()’s, because Temporal’sZonedDateTime.prototype.add()doesn’t acceptdisambiguation/offsetat all. 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.calendar,timeZone, andoffsetare deliberately excluded fromfields— this function only sets date/time components, never the zone or calendar identity, andoffsetis controlled separately viaoptions.offset.overflow(“constrain” (default) | “reject”) controls out-of-range results, e.g. settingmonth: 2on a value whosedayis 31: “constrain” clamps to Feb 29/28, “reject” throws (resulting in “”).- Returns “” for invalid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
zoned ISO 8601 datetime string |
fields |
Omit<Temporal.ZonedDateTimeLike, "calendar" | "timeZone" | "offset"> |
Partial<Temporal.ZonedDateTimeLike> object (excluding calendar/timeZone/offset) specifying fields to set |
Options
Section titled “Options”options
| Option | Type | Default |
|---|---|---|
overflow? |
Overflow |
— |
disambiguation? |
Disambiguation |
— |
offset? |
Offset |
— |
Returns
Section titled “Returns”zoned ISO 8601 string with fields set, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”setZoned("2024-03-10T12:00:00-04:00[America/New_York]", { hour: 9 }) // "2024-03-10T09:00:00-04:00[America/New_York]"setZoned("2024-01-31T12:00:00-05:00[America/New_York]", { month: 2 }) // "2024-02-29T12:00:00-05:00[America/New_York]" (constrain clamps to the last valid day)setZoned("2024-01-31T12:00:00-05:00[America/New_York]", { month: 2 }, { overflow: "reject" }) // ""setZoned("2024-03-10T12:00:00-04:00[America/New_York]", {}) // "2024-03-10T12:00:00-04:00[America/New_York]" (empty fields object is a no-op)setZoned("2024-11-03T01:45:00-05:00[America/New_York]", { minute: 0 }, { disambiguation: "reject" }) // "" (offset defaults to "ignore", so disambiguation actually fires and "reject" throws on this fall-back overlap)setZoned("2024-11-03T01:45:00-05:00[America/New_York]", { minute: 0 }, { disambiguation: "reject", offset: "prefer" }) // "2024-11-03T01:00:00-05:00[America/New_York]" (offset:"prefer" makes disambiguation inert here — the source's -05:00 offset is still valid, so it's kept and "reject" never fires)setZoned("invalid", { hour: 9 }) // ""