The ISO Week definition
The ISO 8601 standard defines week numbers using a Monday-based week (i.e. the week begins on Monday). Under this definition, the first week of the year is determined to be the first week that contains a Wednesday. For example, January 1, 2003 was on a Wednesday; therfore the week it lies in is week number 1 of 2003. However, January 1, 2006 started on a Sunday; under the ISO 8601 week definition, this is considered the 52nd week of 2005.
How the getWeek() function works
getWeek() is a more general week number function that will return the correct week number for any week definition. The argument dowOffset specifies the day of week your locale starts its week on—U.S. users would want to pass a value 0; European and those who want the ISO 8601 standard would pass a value of 1. Of course, you can choose to start the week on any day; getWeek() will define the week numbers based on the third day of the week.
Some users may wonder why the getTimeZoneOffset() function is included in the week number calculation. This was added to prevent duplicate week numbers showing up when daylight savings became active (beginning of April in the U.S.). This was due to the Math.floor() function removing the fractional day number that occurred when taking the difference between the beginning of the year (standard time) and the date (daylight time after April).
You may copy and paste this code without charge. All we ask is you leave the credit line in the function body intact.