@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
18 lines • 1.26 kB
TypeScript
/**
* 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.
*
* @param target ISO ZonedDateTime string to measure distance from
* @param candidates Array of ISO ZonedDateTime strings to choose from
* @returns The nearest candidate zoned datetime string, or "" on invalid input
*
* @example 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]"
* @example 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]"
* @example closestZonedTo("2024-03-15T12:00:00+00:00[UTC]", []) // ""
* @example closestZonedTo("invalid", ["2024-03-01T00:00:00+00:00[UTC]"]) // ""
*/
export declare function closestZonedTo(target: string, candidates: string[]): string;