Skip to content

closestTo value is show in terminal but could not be utilized in the program #3016

Answered by fturmel
emadbaqeri asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @emadbaqeri,

Please refer to the docs to see the proper function usage.

  1. closestTo returns a date, while closestIndexTo returns an index.
  2. The array passed need to be of JS Date instances or timestamps (ECMAScript epoch), not strings.
  3. If the value of the returned index is undefined, it's likely because the array of dates passed to the function was empty.

https://date-fns.org/docs/closestTo
https://date-fns.org/docs/closestIndexTo

import { closestIndexTo } from "date-fns";

const today = new Date();
const dates = [
  new Date("2022-04-10T14:07:12.276Z"),
  new Date("2022-04-10T14:07:06.967Z"),
  new Date("2022-04-10T14:07:04.663Z"),
  new Date("2022-04-10T14:07:03.040Z"),
  new Date("20…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@emadbaqeri
Comment options

Answer selected by emadbaqeri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants