diffUnixAsDuration
Signature
Section titled “Signature”diffUnixAsDuration(value1: number, value2: number, unit: DateTimeDurationUnit, options?: any): stringimport { diffUnixAsDuration } from "@northguild/gmt/unix/calculate";Return the difference between two Unix timestamps as an ISO 8601 duration string,
- bridging to the
durationnamespace (seeparseDuration,normalizeDuration). - Uses Temporal.Instant.until() with
largestUnitset tounit, then.toString(). - Unlike
diffUnix,unitis a single unit (not an array) — an ISO duration string already expresses a full multi-unit breakdown vialargestUnitalone, so there’s no array-of-units overload here. - Returns
""for invalid input (negative diffs are valid and render with a leading-).smallestUnit,roundingIncrement, androundingModecontrol optional rounding of the underlying difference before it’s rendered, per Temporal’s DifferenceOptions — same asdiffUnix.toStringSmallestUnit,fractionalSecondDigits, andtoStringRoundingModecontrol the precision of the rendered string itself, per Temporal’s ToStringPrecisionOptions (mirroringparseDuration’s options) — kept separate from the.until()rounding options above because both option sets have collidingsmallestUnit/roundingModekeys with different Temporal types.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value1 |
number |
first Unix timestamp |
value2 |
number |
second Unix timestamp |
unit |
DateTimeDurationUnit |
DateTimeDurationUnit to use as the duration’s largestUnit |
options |
any |
optional: epochUnit (“seconds” | “milliseconds”), timeZone (IANA), smallestUnit, roundingIncrement, roundingMode (.until() rounding); toStringSmallestUnit, fractionalSecondDigits, toStringRoundingMode (.toString() precision) |
Returns
Section titled “Returns”ISO 8601 duration string, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”diffUnixAsDuration(1706659200000, 1706745600000, "days") // "P1D"diffUnixAsDuration(1706745600000, 1706659200000, "days") // "-P1D"diffUnixAsDuration(1706659200, 1706745600, "days", { epochUnit: "seconds" }) // "P1D"diffUnixAsDuration(NaN, 1706745600000, "days") // ""