7

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.

https://web.archive.org/web/20150906081028/http://techblog.procurios.nl/k/n618/news/view/33796/14863/calculate-iso-8601-week-and-year-in-javascript.html
https://web.archive.org/web/20160402050000/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)

pjvleeuwen
  • 4,215
  • 1
  • 18
  • 31
wubbewubbewubbe
  • 711
  • 1
  • 9
  • 20
  • 1
    Because there were 53 weeks in that year.... – putvande Dec 21 '13 at 16:57
  • 1
    according to the ISO specification, Dec 31 2012 is week 1 of 2013 – wubbewubbewubbe Dec 21 '13 at 17:00
  • 1
    why the downvotes? the question is a valid programming question and well stated. OP might be wrong in the statements, so correct him in the comments or with answers, but don't downvote it. – PA. Dec 21 '13 at 17:05
  • 1
    +1 First I though that you are talking gibberish because common sense would say that a day of 2012 does not belong to a week of 2013. But according to that ISO standard (or the interpretation of it) you are right. Learned something new. – tiguchi Dec 21 '13 at 17:06
  • The notion of week number is dependent on the reference year you take. Your date examples show this ambiguity. You are right about the ISO specification. You might be putting so much faith on the code you found. You must always check and recheck whatever code you use. – PA. Dec 21 '13 at 17:07
  • @putvande actually, there weren't; according to the ISO definition OP is using, 2012 had only 52 weeks. The last 53-week ISO year was 2009. – Mark Reed Dec 21 '13 at 18:04

1 Answers1

4

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.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • After two days struggling, that's fantastic news! Thanks! – wubbewubbewubbe Dec 21 '13 at 17:04
  • 2
    Checked with our Team Calender it says Week 1 of 2013 (It's German one). Mark: All american software is issue to suspect on date calculation and localization since most of Americans can't imagine something outside their continent. Excel took up to version 2003 to deal with week calculations properly and can not handle . and , in CSV files from Germany even in version 2010 without fiddling around in settings. – Axel Amthor Dec 21 '13 at 17:05