New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isoWeek() returns wrong value #2187
Comments
Week 51 => Monday 20 December 2021 - Sunday 26 December 2021 Information
const dayjs = require("dayjs")
const isoWeek = require('dayjs/plugin/isoWeek')
dayjs.extend(isoWeek)
const date = dayjs(new Date(2021, 0, 10, 10, 48, 35))
const date2021week51 = date.isoWeek(51)
console.log(date) // correct, '2021-01-10T09:48:35.000Z'
console.log(date2021week51) // correct, '2021-12-26T09:48:35.000Z' I'll try it on a browser after lunch 😉 |
Information
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/isoWeek.js"></script>
<script>
dayjs.extend(window.dayjs_plugin_isoWeek);
console.log(dayjs().format(), dayjs().isoWeek()); // test dayjs & plugin loaded correctly
const date = dayjs(new Date(2021, 0, 10, 10, 48, 35))
const date2021week51 = date.isoWeek(51)
console.log(date.format()) // correct, '2021-01-10T10:48:35+01:00'
console.log(date2021week51.format()) // correct, '2021-12-26T10:48:35+01:00'
</script> <script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/plugin/isoWeek.js"></script>
<script>
dayjs.extend(window.dayjs_plugin_isoWeek);
console.log(dayjs().format(), dayjs().isoWeek()); // test dayjs & plugin loaded correctly
const year = 2021;
const week = 51;
date = dayjs();
const date2021 = dayjs().year(year);
date2021week51 = dayjs().year(year).isoWeek(week);
console.log(date.format()); // correct, '2023-01-10T15:40:55+01:00'
console.log(date2021.format()); // correct, '2021-01-10T15:40:55+01:00'
console.log(date2021week51.format()); // correct, '2021-12-26T15:40:55+01:00'
</script> I can't reproduce your error !!! |
It was wrong the day I posted it The link for runkit presents the behavior when you run the same code today - it works fine maybe it was related to the fact, that the dajys() returns current date which was then close to the beginning of the year and somehow iso week was calculated wrongly thinking it's still the previous year? |
This is covered here: #1332 When you ran your code (around 1-2 Jan), the week number belonged to the previous year, which is why setting the week number was changing the year. But a few days later (from 3 Jan onwards), the week started belonging to 2023, so the code's behavior changed. The "buggy" behavior can be reproduced like this: Of course this behavior isn't really buggy, but the result of the definition of the ISO week. One way to resolve this is to make sure that the date is set to at least 4th of January before setting the week number since the first week of any year will always contain 4th of January. So something like this: dayjs({year: 2023, day: 4}).isoWeek(week).day(1);
// the year will be 2023 as long as week is between 1 - 52 and the day of the week will be set as Monday Perhaps this should be documented somewhere as this is an easy mistake to make. |
Describe the bug
.isoWeek(week) should set proper week
Expected behavior
.isoWeek(week) sets week from previous year instead
Information
Reproduction code:
https://runkit.com/63b3f5320465cd00088b7bd9/63b3f5e70465cd00088b7c55
The text was updated successfully, but these errors were encountered: