# diffUtc

Industry: Core

## Signature

```ts
diffUtc(value1: string, value2: string, units: keyof DurationLikeObject | DateTimeUnit | (keyof DurationLikeObject | DateTimeUnit)[], options?: RoundingOptions<DateTimeUnit>): number | Record<keyof DurationLikeObject, number> | null
```

Return the difference between two UTC datetimes measured in the given date-time unit.

- Converts both values to a `Temporal.ZonedDateTime` in UTC and uses its `until()`, so calendar units (days, weeks, months, years) are measured on the UTC wall clock — which has no DST, so every day is 24 hours — while time units are exact elapsed time. `Temporal.Instant.until()` is not used: it rejects every calendar `largestUnit`.
- Supports single unit or array of units.
- Returns null for invalid input. `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the result, per Temporal's DifferenceOptions — e.g. `{ smallestUnit: "hour", roundingMode: "halfExpand" }` rounds the difference to the nearest hour before extracting the requested unit.
- When `units` is an array, `smallestUnit` must not be coarser than the largest unit in the array (e.g. `["days", "hours"]` with `smallestUnit: "week"`) — this combination is rejected by Temporal and returns null, same as other invalid input.
- With an array of units, the record is the whole difference: the largest listed unit is Temporal's `largestUnit`, and the amount of each unlisted unit between two listed units is carried into the next smaller listed unit, measured from the start moved by the larger listed amounts (so adding the record to the start reaches the end). Units smaller than the smallest listed unit are truncated, as for a single unit. For example, `["years", "days"]` over 1 year 59 days returns `{ years: 1, days: 59 }`.
- Nanosecond precision: the result is a JavaScript `number`, so it stops being exact once it passes `Number.MAX_SAFE_INTEGER` — about 104 days in nanoseconds and about 285 years in microseconds. `diffUtc("2024-01-01T00:00:00Z", "2024-04-15T00:00:00.000000001Z", "nanoseconds")` returns `9072000000000000` rather than `9072000000000001`. For an exact count use the `bigint` APIs: `spanNs` in `span/`, or `toNanoseconds` in `precision/`.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `value1` | `string` | UTC ISO datetime string (start) |
| `value2` | `string` | UTC ISO datetime string (end) |
| `units` | `keyof DurationLikeObject` \| `DateTimeUnit` \| `(keyof DurationLikeObject \| DateTimeUnit)[]` | DateTimeDurationUnit \| DateTimeDurationUnit[] to measure the difference |

## Options

**options**

optional: how the difference is rounded; a non-object value (such as `null`) is invalid

Type: [`RoundingOptions`](/gmt/reference/types/RoundingOptions/).

| 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. |

## Returns

numeric difference in the requested unit, or null on invalid input

## Related types

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

## Examples

```ts
import { diffUtc } from "@northguild/gmt/utc/calculate";

diffUtc("2024-03-10T12:00:00Z", "2024-03-11T12:00:00Z", "hours") // 24
```

## Source

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