Skip to content
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

Open
Daszczu opened this issue Jan 3, 2023 · 4 comments
Open

isoWeek() returns wrong value #2187

Daszczu opened this issue Jan 3, 2023 · 4 comments

Comments

@Daszczu
Copy link

Daszczu commented Jan 3, 2023

Describe the bug
.isoWeek(week) should set proper week

Expected behavior
.isoWeek(week) sets week from previous year instead

Information

  • Day.js Version v1.11.7
  • OS: Windows
  • Browser: Chrome 108
  • Time zone: GMT +1

Reproduction code:

const dayjs = require("dayjs");
var isoWeek = require('dayjs/plugin/isoWeek')
dayjs.extend(isoWeek);

year = 2021;
week = 51;

date = dayjs();
date2021 = dayjs().year(year);
date2021week51 = dayjs().year(year).isoWeek(week);
    
console.log(date); // correct
console.log(date2021); // correct 
console.log(date2021week51); // incorrect - should be week 51 of year 2021, not 2020

https://runkit.com/63b3f5320465cd00088b7bd9/63b3f5e70465cd00088b7c55

@KronosDev-Pro
Copy link

Week 51 => Monday 20 December 2021 - Sunday 26 December 2021

Information

  • Day.js v1.11.7
  • OS: Windows 11
  • Node: v18.7.0
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 😉

@KronosDev-Pro
Copy link

KronosDev-Pro commented Jan 10, 2023

Information

  • Day.js v1.11.7
  • OS: Windows 11
  • Browser: Brave v108 (Chromium v108)
<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 !!!

@Daszczu
Copy link
Author

Daszczu commented Jan 11, 2023

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?
that I don't know, but I could observe this isssue during that day on multiple devices and platforms

@20manas
Copy link

20manas commented Sep 12, 2023

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:

image

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants