closestZonedTo
Signature
Section titled “Signature”closestZonedTo(target: string, candidates: string[]): stringimport { closestZonedTo } from "@northguild/gmt/zoned/calculate";Return the candidate zoned datetime nearest to target by temporal distance.
- Distance is measured in total days using
Temporal.Instantepoch milliseconds. - Returns
nullif the candidates array is empty or contains no valid dates. - Returns
nulliftargetis invalid. - On a tie (two equidistant candidates), returns the first one in array order.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
target |
string |
ISO ZonedDateTime string to measure distance from |
candidates |
string[] |
Array of ISO ZonedDateTime strings to choose from |
Returns
Section titled “Returns”The nearest candidate zoned datetime string, or null on invalid input
Examples
Section titled “Examples”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]"closestZonedTo("2024-03-15T12:00:00+00:00[UTC]", ["2024-03-01T00:00:00+00:00[UTC]", "2024-03-29T00:00:00+00:00[UTC]"]) // "2024-03-29T00:00:00+00:00[UTC]"closestZonedTo("2024-03-15T12:00:00+00:00[UTC]", []) // nullclosestZonedTo("invalid", ["2024-03-01T00:00:00+00:00[UTC]"]) // null