dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

getWeekOfYear changes week number on Sunday - is this ISO?

Open DNA-PC opened this issue 3 years ago • 3 comments

Describe the bug Calling getWeekOfYear("2023-08-28") returns 35, I would have expected 34.

It all comes down to whether we consider when the week starts, and I thought the ISO standard was to start on Monday. Am I wrong?

I don't necessarily want things to change, but I wondered whether it was a bug, and will this behavior ever change, or can it be related to the device (browser locale, etc.)

DNA-PC avatar Aug 29 '23 19:08 DNA-PC

After reading the documentation, I came to the conclusion that this is how it should work, since their example uses:

var weekOfYear = require('dayjs/plugin/weekOfYear')
dayjs.extend(weekOfYear)

dayjs('2018-06-27').week() // 26
dayjs('2018-06-27').week(5) // set week

and by this logic it should have returned 25, but it returns 26.

VladyslavYe avatar Aug 30 '23 08:08 VladyslavYe

Hi, @DNA-PC. At first I think you mentioned wrong date, 2023-08-28 is 35th week (https://www.epochconverter.com/weeks/2023), should be 2023-08-27. By default dayjs('2023-08-27') returns 35 but should return 34. There are 2 options of achieving the expected result:

  1. just use isoWeek plugin (https://day.js.org/docs/en/plugin/iso-week) dayjs('2023-08-27').isoWeek() // 34
  2. use updateLocale plugin (https://day.js.org/docs/en/customization/customization) with such configuration:
dayjs.updateLocale('en', {
  weekStart: 1,
  yearStart: 4
})

dayjs('2023-08-27').week() // 34

I don't know why but you should necessarily pass yearStart, without this prop this method will not work. But pay attention that this method is global and will affect all dayjs calls.

Pareder avatar Aug 31 '23 07:08 Pareder

Thanks and indeed, I mentioned the wrong date, I did mean 2023-08-27, which is a Sunday. I'm building a software where weeks must start on Sundays, not on Mondays.

With my current dayjs configuration, weeks start on Sundays, but I was surprised it worked as I wanted, because the software I'm using (WeWeb) is French, and in France, weeks start on Mondays, so I want to enquire whether that was the native dayjs behavior or not.

Thank you for those precisions, I'll take it over with the WeWeb team from there!

DNA-PC avatar Aug 31 '23 09:08 DNA-PC