# diffZonedAsDuration

Industry: Core

## Signature

```ts
diffZonedAsDuration(value1: string, value2: string, unit: keyof DurationLikeObject | DateTimeUnit, options?: (RoundingOptions<DateTimeUnit> & DurationStringOptions)): string
```

Return 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 `largestUnit` set to `unit`, 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 calendar `unit` (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 (TC39 `CalendarEquals`; before 1.16.0 measured in ISO).
- Unlike `diffZoned`, `unit` is a single unit (not an array) — an ISO duration string already expresses a full multi-unit breakdown via `largestUnit` alone, so there's no array-of-units overload here.
- Returns `""` for invalid input (negative diffs are valid and render with a leading `-`). `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the underlying difference before it's rendered, per Temporal's DifferenceOptions — same as `diffZoned`. `toStringSmallestUnit`, `fractionalSecondDigits`, and `toStringRoundingMode` control the precision of the rendered string itself, per Temporal's ToStringPrecisionOptions (mirroring `parseDuration`'s options) — kept separate from the `.until()` rounding options above because both option sets have colliding `smallestUnit`/`roundingMode` keys 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); see `isValidCalendarZonedDateTime`.

## 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 DurationLikeObject` \| `DateTimeUnit` | date-time unit to use as the duration's largestUnit, singular or plural (`"day"` or `"days"`) |

## 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?` | `"years"` \| `"months"` \| `"weeks"` \| `"days"` \| `"hours"` \| `"minutes"` \| `"seconds"` \| `"milliseconds"` \| `"microseconds"` \| `"nanoseconds"` \| `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?` | `PluralizeUnit<"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?` | `"ceil"` \| `"floor"` \| `"expand"` \| `"trunc"` \| `"halfCeil"` \| `"halfFloor"` \| `"halfExpand"` \| `"halfTrunc"` \| `"halfEven"` | `"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

ISO 8601 duration string, or "" on invalid input

## Related types

- [`DateTimeDurationUnit`](/gmt/reference/types/DateTimeDurationUnit/)
- [`RoundingOptions`](/gmt/reference/types/RoundingOptions/)
- [`DurationStringOptions`](/gmt/reference/types/DurationStringOptions/)

## Examples

```ts
import { diffZonedAsDuration } from "@northguild/gmt/zoned/calculate";

diffZonedAsDuration("2024-10-05T12:00:00+10:00[Australia/Sydney]", "2024-10-07T12:00:00+11:00[Australia/Sydney]", "days") // "P2D" (wall-clock days; 47 elapsed hours)
```

## Source

[zoned/calculate/diffZonedAsDuration.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/zoned/calculate/diffZonedAsDuration.ts)
