setUtc
Signature
Section titled “Signature”setUtc(value: string, fields: Omit<ZonedDateTimeLike, "calendar" | "era" | "eraYear" | "offset" | "timeZone">, options?: { overflow?: "reject" | "constrain" | undefined; }): stringReturn 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,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, anoffset, 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. - There are no
disambiguationoroffsetoptions (removed in 1.16.0): a UTC instant’s wall clock is never ambiguous and its offset is always +00:00, so neither had anything to act on. - 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<ZonedDateTimeLike, "calendar" | "era" | "eraYear" | "offset" | "timeZone"> | Partial<Temporal.ZonedDateTimeLike> object (excluding calendar/timeZone/offset) specifying fields to set |
Options
Section titled “Options”options
How an out-of-range result is handled
| Option | Type | Default | Description |
|---|---|---|---|
overflow? | "reject" | "constrain" (Overflow) | "constrain", Temporal’s default. | What to do when the result is not a real date. "constrain" clamps it to the last valid day, so month: 2 on Jan 31 is Feb 29/28 (TC39 Temporal); "reject" returns "". |
Returns
Section titled “Returns”UTC Instant string with fields set, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { setUtc } from "@northguild/gmt/utc/calculate";
setUtc("2024-03-10T12:00:00Z", { hour: 9 }) // "2024-03-10T09:00:00Z"