Return an index of the closest date from the array comparing to the given date.
Return an index of the closest date from the array comparing to the given date.
// CommonJS
var closestIndexTo = require('date-fns/closestIndexTo')
// ES 2015 import closestIndexTo from 'date-fns/closestIndexTo'
// ESM
import { closestIndexTo } from 'date-fns'
closestIndexTo(dateToCompare, datesArray)
| Name | Description |
|---|---|
dateToCompare |
the date to compare with |
datesArray |
the array to search |
| Description |
|---|
|
an index of the date 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? const dateToCompare = new Date(2015, 8, 6) const datesArray = [ new Date(2015, 0, 1), new Date(2016, 0, 1), new Date(2017, 0, 1) ] const result = closestIndexTo(dateToCompare, datesArray) //=> 1
© 2021 Sasha Koss and Lesha Koss
Licensed under the MIT License.
https://date-fns.org/v2.29.2/docs/closestIndexTo