diffZonedAsDuration
Signature
Section titled “Signature”diffZonedAsDuration(value1: string, value2: string, unit: keyof DurationLike | DateTimeUnit, options?: (RoundingOptions<DateTimeUnit> & DurationStringOptions)): stringReturn the difference between two zoned datetimes as an ISO 8601 duration string, bridging to the duration namespace (see parseDuration, normalizeDuration).
- Uses Temporal.ZonedDateTime.until with
largestUnitset tounit, then.toString(). - Measures like
diffZoned(Temporal’s DifferenceZonedDateTime): calendar units on the zone’s own wall clock, time units in exact elapsed time. Values in different time zones with a calendarunit(days or larger) return "". - Compatibility: before 1.16.0 both values were converted to UTC first. To get that value:
diffUtcAsDuration(convertZonedToUtc(value1), convertZonedToUtc(value2), unit). - Accepts RFC 9557 calendar-annotated zoned strings, with the same calendar rule as
diffZoned: measured in the shared calendar,""when the two name different calendars (TC39CalendarEquals; before 1.16.0 measured in ISO). - Unlike
diffZoned,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 asdiffZoned.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. - Compatibility: since 1.16.0 calendar strings are RFC 9557 (ISO digits, the
[u-ca=<id>]annotation after the zone, canonical calendar ids); seeisValidCalendarZonedDateTime.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value1 | string | zoned ISO 8601 datetime string (start), optionally calendar-annotated |
value2 | string | zoned ISO 8601 datetime string (end), optionally calendar-annotated |
unit | keyof DurationLike | DateTimeUnit | date-time unit to use as the duration’s largestUnit, singular or plural ("day" or "days") |
Options
Section titled “Options”options
optional: how the difference is rounded and how the string is rendered; a non-object value (such as null) is invalid
| Option | Type | Default | Description |
|---|---|---|---|
smallestUnit? | DateTimeUnit | PluralUnit<DateTimeUnit> | None. The difference is not rounded: Temporal uses the smallest unit the values have, "day" for dates and "nanosecond" otherwise. | The smallest unit kept in the difference; anything smaller is rounded into it. It may not be larger than the largest unit asked for. |
roundingIncrement? | number | 1, Temporal’s default. | The step the smallest unit is rounded to, so 15 with "minute" rounds to a quarter of an hour. For a time unit it must divide the next larger unit evenly and be smaller than it (15 or 30 minutes, not 7 or 60); a date unit takes any whole number from 1 up. A non-integer is truncated first, as Temporal does, and any other value is invalid input. |
roundingMode? | "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | "trunc", Temporal’s default for a difference. | Which way a value between two steps goes. "trunc" rounds toward zero and "halfExpand" to the nearest step, a half going away from zero; the other modes are those of Temporal’s roundingMode option. |
toStringSmallestUnit? | SmallestUnit<"minute" | "second" | "millisecond" | "microsecond" | "nanosecond"> | None. The precision comes from fractionalSecondDigits. | The smallest unit written in the string: "second", "millisecond", "microsecond" or "nanosecond". It sets how many fractional-second digits appear and takes precedence over fractionalSecondDigits. "minute" is refused, as Temporal.Duration refuses it. |
fractionalSecondDigits? | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | "auto" | 8 | 9 | "auto", Temporal’s default. | The number of fractional-second digits written: 0 to 9 writes exactly that many, and "auto" writes as many as the duration needs, with no trailing zeros. It has no effect when toStringSmallestUnit is given. |
toStringRoundingMode? | RoundingMode | "trunc", Temporal’s default. | How the duration is rounded when the precision asked for drops digits. "trunc" cuts them off; "halfExpand" rounds to the nearest value, a half going away from zero. |
Returns
Section titled “Returns”ISO 8601 duration string, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { diffZonedAsDuration } from "@northguild/gmt/zoned/calculate";
diffZonedAsDuration("2024-03-09T12:00:00-05:00[America/New_York]", "2024-03-11T12:00:00-04:00[America/New_York]", "days") // "P2D" (wall-clock days; 47 elapsed hours)