setUtc
Signature
Section titled “Signature”setUtc(value: string, fields: Omit<Temporal.ZonedDateTimeLike, "calendar" | "timeZone" | "offset">, options?: { overflow?: Overflow; disambiguation?: Disambiguation; offset?: Offset; }): stringimport { setUtc } from "@northguild/gmt/utc/calculate";Return a UTC Instant string with the given fields set on value.
- Converts to ZonedDateTime (fixed timeZone “UTC”), wraps
Temporal.ZonedDateTime.prototype.with()(resolving every supplied field in a single atomic overflow pass), then converts back to an Instant. This is the safe alternative to composingaddUtc()calls field-by-field — seesetDate’s doc for why order-independent field resolution matters. 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.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 “”).disambiguationandoffsetare accepted for API consistency withsetZoned/setUnix, but are permanently inert here: “UTC” has no DST transitions, so there is never an ambiguous local time or a stale offset for either option to act on (same precedent asstartOfUtc/endOfUtc, Story C1/C2). Do not “fix” this by removing them.- Returns “” for invalid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
string |
ISO UTC datetime string (e.g. “2024-03-10T12:00:00Z”) |
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”UTC Instant string with fields set, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”setUtc("2024-03-10T12:00:00Z", { hour: 9 }) // "2024-03-10T09:00:00Z"setUtc("2024-01-31T12:00:00Z", { month: 2 }) // "2024-02-29T12:00:00Z" (constrain clamps to the last valid day)setUtc("2024-01-31T12:00:00Z", { month: 2 }, { overflow: "reject" }) // ""setUtc("2024-03-10T12:00:00Z", {}) // "2024-03-10T12:00:00Z" (empty fields object is a no-op)setUtc("invalid", { hour: 9 }) // ""