2

What is the most proper way of comparing two dates, I have a holidays array from google, and dates fetched from react-day-picker, I transform them to be in JSON format with date.toJSON() method and with the dates from google I get following output:

0: "2020-12-24T00:00:00.000Z"
1: "2020-12-25T00:00:00.000Z"
2: "2021-04-04T00:00:00.000Z"
3: "2022-04-17T00:00:00.000Z"

Their time is zeroed and this is what I would like to end up with, however, when I run .toJSON() method on dates from react-day-picker I get

2021-10-21T10:00:00.000Z
2021-10-27T10:00:00.000Z
2021-10-28T10:00:00.000Z
2021-11-03T11:00:00.000Z
2021-11-04T11:00:00.000Z

they're with the locale time, and then I use date-fns to zero the hours with the set() method like so:

day = set(day, { hours: 0 }).toJSON();
2021-10-19T22:00:00.000Z
2021-10-20T22:00:00.000Z
2021-10-26T22:00:00.000Z
2021-10-27T22:00:00.000Z
2021-11-02T23:00:00.000Z
2021-11-03T23:00:00.000Z
2021-11-09T23:00:00.000Z

so I guess I am unable to get rid of the locale burden, and compare the dates? Or is there a method that could do that for me? Thanks a lot

seven
  • 1,183
  • 14
  • 33
  • What is the format of the dates "from Google"? What is the format of the dates from react day picker? – RobG Oct 14 '21 at 20:16
  • @RobG I understand the point. From google: '2020-12-25', from react-date-picker: Tue Oct 19 2021 12:00:00 GMT+0200 (Central European Summer Time) – seven Oct 16 '21 at 15:15
  • Probably a duplicate of [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Oct 16 '21 at 21:41

0 Answers0