closestDateTo
Signature
Section titled “Signature”closestDateTo(target: string, candidates: string[]): stringReturn the candidate date nearest to target by calendar distance.
- Distance is the absolute whole-day count
Math.abs(target.until(candidate).days).Temporal.PlainDate.compareis not used for it:compareorders two dates (-1,0,1), it does not measure between them.until()on aPlainDatedefaults tolargestUnit: "day", so the count runs across month and year boundaries —2024-03-15to2026-01-01is657. - Returns
""if the candidates array is empty or contains no valid dates, ortargetis invalid. - Compatibility: before 1.16.0 invalid input returned
null. - 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 "" on invalid input
Examples
Section titled “Examples”import { closestDateTo } from "@northguild/gmt/plain/calculate";
closestDateTo("2024-03-15", ["2024-03-01", "2024-03-20", "2024-03-18"]) // "2024-03-18"