closestDateTo
Signature
Section titled “Signature”closestDateTo(target: string, candidates: string[]): stringimport { closestDateTo } from "@northguild/gmt/plain/calculate";Return the candidate date nearest to target by calendar distance.
- Distance is measured in whole days using
Temporal.PlainDate.compare. - 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 PlainDate string to measure distance from |
candidates |
string[] |
Array of ISO PlainDate strings to choose from |
Returns
Section titled “Returns”The nearest candidate date string, or null on invalid input
Examples
Section titled “Examples”closestDateTo("2024-03-15", ["2024-03-01", "2024-03-20", "2024-03-18"]) // "2024-03-18"closestDateTo("2024-03-15", ["2024-03-01", "2024-03-29"]) // "2024-03-01"closestDateTo("2024-03-15", []) // nullclosestDateTo("invalid", ["2024-03-01"]) // null