UNPKG

1.03 kBTypeScriptView Raw
1/**
2 * @name closestTo
3 * @category Common Helpers
4 * @summary Return a date from the array closest to the given date.
5 *
6 * @description
7 * Return a date from the array closest to the given date.
8 *
9 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
10 *
11 * @param dateToCompare - The date to compare with
12 * @param dates - The array to search
13 *
14 * @returns The date from the array closest to the given date or undefined if no valid value is given
15 *
16 * @example
17 * // Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030?
18 * const dateToCompare = new Date(2015, 8, 6)
19 * const result = closestTo(dateToCompare, [
20 * new Date(2000, 0, 1),
21 * new Date(2030, 0, 1)
22 * ])
23 * //=> Tue Jan 01 2030 00:00:00
24 */
25export declare function closestTo<DateType extends Date>(
26 dateToCompare: DateType | number | string,
27 dates: Array<DateType | number | string>,
28): DateType | undefined;