you-dont-need / You-Dont-Need-Momentjs

List of functions which you can use to replace moment.js + ESLint Plugin
MIT License
13.27k stars 318 forks source link

IsSame example is not equivalent #103

Open Eonasdan opened 3 years ago

Eonasdan commented 3 years ago

Hello, thanks for this great resource. I'm working on a new version of my date picker and I'm looking to remove momentjs. I was able to use some of the info here to whittle down the functions I needed. You are welcome to look at the full example on stackblitz as make use of what's there.

One thing I noticed is that your IsSame example with a granularity is not equivalent: new Date(2010, 9, 20).toDateString().substring(4, 7) === new Date(2010, 9, 21).toDateString().substring(4, 7);

While technically does verify that both dates are in Oct this is not what moment/dayjs do. Those libs use their startOf function to take the dates to the beginning of the unit passed and then compare the valueOf. (I think the comparison of dates example should use valueOf as that's a more accurate comparison anyway).

In the provide example moment and dayjs would do the following

moment('2010-10-20').isSame('2010-10-21', 'month');
thisDate = 2010-10-1 //via startOf
compareDate = 2010-10-1
thisDate.valueOf() === compareDate.valueOf() //true

moment('2010-10-20').isSame('2021-10-21', 'month');
thisDate = 2010-10-1
compareDate = 2021-10-1
thisDate.valueOf() === compareDate.valueOf() //false