# closestZonedTo

Industry: Core

## Signature

```ts
closestZonedTo(target: string, candidates: string[]): string
```

Return the candidate zoned datetime nearest to `target` by temporal distance.

- Distance is measured in total days using `Temporal.Instant` epoch milliseconds.
- Returns `""` if the candidates array is empty or contains no valid dates, or `target` is invalid.
- **Compatibility:** before 1.16.0 invalid input returned `null`.
- On a tie (two equidistant candidates), returns the first one in array order.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `target` | `string` | ISO ZonedDateTime string to measure distance from |
| `candidates` | `string[]` | Array of ISO ZonedDateTime strings to choose from |

## Returns

The nearest candidate zoned datetime string, or "" on invalid input

## Examples

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

closestZonedTo("2024-03-15T12:00:00+00:00[UTC]", ["2024-03-01T00:00:00+00:00[UTC]", "2024-03-20T00:00:00+00:00[UTC]", "2024-03-18T00:00:00+00:00[UTC]"]) // "2024-03-18T00:00:00+00:00[UTC]"
```

## Source

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