dayjs
dayjs copied to clipboard
Year + Week wrong calculation
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)
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 ?
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!
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 🎉
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
I understand the problem better. I'll try to look into it as soon as possible
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')