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
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
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...
Am I missing something? What bothers me is that it occurs on only so few dates (3 dates in 70 years or more)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With