setUnix
Signature
Section titled “Signature”setUnix(value: number, fields: Omit<Temporal.ZonedDateTimeLike, "calendar" | "timeZone" | "offset">, options?: { epochUnit?: "seconds" | "milliseconds"; timeZone?: string; overflow?: Overflow; disambiguation?: Disambiguation; offset?: Offset; }): numberimport { setUnix } from "@northguild/gmt/unix/calculate";Return a Unix epoch value with the given fields set on value, interpreted in timeZone.
- Converts to ZonedDateTime, wraps
Temporal.ZonedDateTime.prototype.with()(resolving every supplied field in a single atomic overflow pass), then converts back to epoch. This is the safe alternative to composingaddUnix()calls field-by-field — seesetZoned’s doc for why order-independent field resolution matters, and whydisambiguation/offsetrequire.with()rather than arithmetic. 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 null).- Returns null for invalid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value |
number |
Unix timestamp (number) |
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 |
|---|---|---|
epochUnit? |
"seconds" | "milliseconds" |
— |
timeZone? |
string |
— |
overflow? |
Overflow |
— |
disambiguation? |
Disambiguation |
— |
offset? |
Offset |
— |
Returns
Section titled “Returns”Unix epoch number with fields set, or null on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”setUnix(1710072000000, { hour: 9 }, { timeZone: "UTC" }) // 1710061200000 (2024-03-10T09:00:00Z)setUnix(1706659200000, { year: 2025 }, { timeZone: "UTC" }) // 1738281600000 (2025-01-31T00:00:00Z)setUnix(1706659200000, {}, { timeZone: "UTC" }) // 1706659200000 (empty fields object is a no-op)setUnix(NaN, { hour: 9 }) // null