Return a date from the array closest to the given date.
Return a date from the array closest to the given date.
// CommonJS
var closestTo = require('date-fns/closestTo')
// ES 2015 import closestTo from 'date-fns/closestTo'
// ESM
import { closestTo } from 'date-fns'
closestTo(dateToCompare, datesArray)
| Name | Description |
|---|---|
dateToCompare |
the date to compare with |
datesArray |
the array to search |
| Description |
|---|
|
the date from the array closest to the given date or undefined if no valid value is given |
| Type | Description |
|---|---|
TypeError |
2 arguments required |
// Which date is closer to 6 September 2015: 1 January 2000 or 1 January 2030? const dateToCompare = new Date(2015, 8, 6) const result = closestTo(dateToCompare, [ new Date(2000, 0, 1), new Date(2030, 0, 1) ]) //=> Tue Jan 01 2030 00:00:00
© 2021 Sasha Koss and Lesha Koss
Licensed under the MIT License.
https://date-fns.org/v2.29.2/docs/closestTo