Skip to content

closestZonedTo

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.
ParameterTypeDescription
targetstringISO ZonedDateTime string to measure distance from
candidatesstring[]Array of ISO ZonedDateTime strings to choose from

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

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]"
candidates
output 

zoned/calculate/closestZonedTo.ts