dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Year + Week wrong calculation

Open fullbl opened this issue 1 year ago • 6 comments

Describe the bug Using dayjs setting year and week brings wrong date

Expected behavior Setting dayjs().year(2024).week(1) should give you a day in the first week of 2024. It gives you a day in 2025 instead. Everything returns cool if you reset the month (e.g. dayjs().year(2024).startOf('year').week(1))

Information

  • Day.js Version: v1.11.10
  • OS: Ubuntu
  • Browser: Firefox 120.0.1 (64 bit)
  • Time zone: GMT+01:00 (Rome)

fullbl avatar Dec 31 '23 10:12 fullbl

Hello @fullbl 😄 I've just tried to reproduce your problem, except that I get the expected result. Here's a Stackblitz link with the code. Can you tell us more so that we can help you and fix the issue ?

Nekxio avatar Jan 03 '24 16:01 Nekxio

Hi! Thanks for the reply! I think the error was present while we still were in 2023!

I updated the Stackblitz to reflect the error! (the date is different to the one that was proposed in my code, but still is not the first week of 2024!

PS: Happy new year!

fullbl avatar Jan 03 '24 16:01 fullbl

I can't access your project. Can you send the editor's link instead of the application ? I've updated mine to match your time zone, if that helps. Happy New Year 🎉

Nekxio avatar Jan 03 '24 17:01 Nekxio

https://stackblitz.com/edit/typescript-beubmj?file=index.ts sorry, I didn't know about how stackblitz works, however I just changed how the dayjs object was constructed:

console.log(dayjs('2023-12-31').year(2024).week(1).utc().local().format()); => 2024-12-31T00:00:00+01:00
console.log(dayjs('2023-12-31').year(2024).startOf('year').week(1).utc().local().format()); => 2024-01-01T00:00:00+01:00

fullbl avatar Jan 03 '24 17:01 fullbl

I understand the problem better. I'll try to look into it as soon as possible

Nekxio avatar Jan 03 '24 17:01 Nekxio

After checking the code, I saw that the case of the end of the year was already made.

If i'm right, this calculates the first day of the following year and obtains the last day of the given week. If the first day of the following year is before the end of the current week, this means that December 31 is in the first week of the following year, so the week method returns 1.

So if you want to check from the beginning of the year, you need to add .startOf('year')

Nekxio avatar Jan 04 '24 15:01 Nekxio