Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain why Dec 31 2012 is week 53 and not week 1 (ISO-8601)

I'm trying to write a function in JavaScript that returns the week number for a given date and I'm testing my code against two functions I've found on the web, i.e.

http://techblog.procurios.nl/k/n618/news/view/33796/14863/calculate-iso-8601-week-and-year-in-javascript.html
http://www.epoch-calendar.com/support/getting_iso_week.html

I've tested 30,000 days starting from Jan 1 1970 and I got a few differences with the second source. The first kind of differences is where that source returns week zero for some cases, which is clearly wrong. Then for some other cases it return week 53, where my function returns week 1. These are the dates

  • 1984, Dec 31
  • 2012, Dec 31
  • 2040, Dec 31

And they all look like this

Dec         Jan
29  30  31  01  02  03  04  05  06  07  08  09  10
Sa  Su  Mo  Tu  We  Th  Fr  Sa  Su  Mo  Tu  We  Th
        ^^                          ^^

according to ISO-8601

  • weeks start on Monday
  • first week of the year is the week with the first Thursday of that year

Jan 3 is the first Thursday of 2013, therefore Jan 3 is a day in week 1 (of 2013). And the start of week 1 (of 2013) is the Monday before that Thursday, which is Dec 31 2012.

Therefore Dec 31 2012 must be week 1.

However...

  • when I type 'what week is 31 december 2012' in Wolfram Alpha, it returns week 53
  • when I type '=WEEKNUM("2012-12-31")' in excel, it also returns week 53

Am I missing something? What bothers me is that it occurs on only so few dates (3 dates in 70 years or more)

like image 796
wubbewubbewubbe Avatar asked Dec 21 '13 16:12

wubbewubbewubbe


1 Answers

Not everything uses ISO 8601; there are many differing definitions of "week number". You are correct in your interpretation - 31 Dec 2012 was indeed the start of ISO week 1 of 2013.

Demonstration using GNU's date(1) command on Linux:

$ date +%GW%V -d '2012-12-31'
2013W01

If you want to use the ISO definition in Excel, you have to specify return type 21:

=WEEKNUM("2012-12-31",21)

yields 1.

like image 88
Mark Reed Avatar answered Oct 14 '22 09:10

Mark Reed