0

I need to determine how many ISO weeks a year has.

My attempt looks like this

moment.tz(2019, "YYYY", "UTC").endOf("year").isoWeek()

But that doesn't do the job. 31st of December is ISO week 1 in some cases.

Ofc, there are probably some ugly ways to get the result, but i would like a clean code.

Necky
  • 70
  • 8
  • 1
    Why do you need to do that in UTC or ISO? Won't the weeks in a year be same irrespective of the timezone and date-time format? – Shahzad Nov 23 '20 at 14:50
  • A year has [52 weeks](https://crm.org/articles/how-many-weeks-in-a-year)...?! Give or take a day in a common year or two in a leap year, but isn't the answer always 52? – Jeremy Thille Nov 23 '20 at 14:58
  • Have a look at this question: [Get weeks in year](https://stackoverflow.com/q/18478741/4131048) – VincenzoC Nov 23 '20 at 15:05
  • 1
    @Shahzad No, ISO weeks are different. "ISO week year numbering therefore slightly deviates from the Gregorian for some days close to 1 January" - Source: https://en.wikipedia.org/wiki/ISO_week_date – Necky Nov 25 '20 at 07:47
  • 1
    @JeremyThille Unfortunately, that's not the case for ISO weeks. A year can have 52 or 53 of them. – Necky Nov 25 '20 at 07:49
  • @VincenzoC Thank you! It seems to work. I didn't find this post before :/ I thought there would be a nice way in the moment library – Necky Nov 25 '20 at 07:53

1 Answers1

0

It doesn't seem like moment.js provides an interface for this.

This seems to be the solution (pure JS) Get weeks in year

Necky
  • 70
  • 8