closestTo()
Given an array of dates, closestTo() returns an object containing data of the compareDate closest to the controlDate.
Usage
import { closestTo } from 'easy-dates';
closestTo(controlDate, compareDate) 
The expected output is an object with the following properties:
- difference: the time in milliseconds between the- controlDateand the date closest in time from the- compareDateargument.
- closest: the date object of the closest date from the- compareDate.
- closestIndex: the index of the closest date in the- compareDatearray.
Prop types
| prop | type | required | default | 
|---|---|---|---|
| controlDate | date | yes | null | 
| compareDate | array | yes | null | 
Examples
Compare multiple dates
const twentyFourHours = 86400000;
const today = new Date(Date.now());
const tomorrow = new Date(Date.now() + twentyFourHours);
const dayAfterTomorrow = new Date(Date.now() + twentyFourHours * 2);
const weekAfterTomorrow = new Date(Date.now() + twentyFourHours * 7);
const argumentsArray = [tomorrow, dayAfterTomorrow, weekAfterTomorrow];
closestTo(today, argumentsArray) // { difference: 85400000, closest: Sat Mar 19 2022 20:26:34 GMT-0400 (Eastern Daylight Time), closestIndex: 0 }
Compare a single date
const twentyFourHours = 86400000;
const today = new Date(Date.now());
const tomorrow = new Date(Date.now() + twentyFourHours);
closestTo(today, [tomorrow]) // { difference: 85400000, closest: Sat Mar 19 2022 20:26:34 GMT-0400 (Eastern Daylight Time), closestIndex: 0 }
Comparing a single date?
Don't forget that the compareDate must be an array.