Hacker0x01 / react-datepicker

A simple and reusable datepicker component for React
https://reactdatepicker.com/
MIT License
8.06k stars 2.24k forks source link

Shown week numbers are incorrect for en-US locale #3507

Open stefanorie opened 2 years ago

stefanorie commented 2 years ago

Describe the bug When using "en-US" as the locale, the wrong week is shown inside the calendar. For example, this week (4th of March, week 9) will show week 8.

I think this happens because the util getWeek actually uses getISOWeek from date-fns:https://github.com/Hacker0x01/react-datepicker/blob/aff090fedb1b20e8973e6ff12d337df4b28ae3ea/src/date_utils.js#L218

But when getting the start of the week, startOfWeek is used instead of startOfISOWeek. https://github.com/Hacker0x01/react-datepicker/blob/aff090fedb1b20e8973e6ff12d337df4b28ae3ea/src/date_utils.js#L235

In case of this current week, for en-US this means it will take the 27th of February as the start of the week. After that it will get the actual week number via getISOWeek, which will result in week 8. I think this is because ISO weeks start on Monday and end on Sunday. So now it takes the last day of the previous week as the week number.

This can also be tested inside the console when you go to the date-fns website:

Click to expand! ![image](https://user-images.githubusercontent.com/2376493/156767430-a640eba3-e24e-4bb3-bbf5-00bd47fcb048.png)

To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/react-datepicker-forked-gnzrpu?file=/src/index.js
  2. Open the calendar
  3. Navigate to 4th of March 2022 and see that it shows week 8 instead of 9

Expected behavior I expected that I would see week 9 instead of week 8, because it should actually show the ISO week.

Additional context I have cloned the repository and made the fix locally, however 10 tests will fail then. So I'm not 100% sure if the tests should be adjusted, or if applying startOfISOWeek will actually break things.

github-actions[bot] commented 3 months ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.