getISOWeek() in JavaScriptWhile the JavaScript Date object is one of the most complete of all the default date-handling routines in programming languages, one of the features it is sorely lacking is an algorithm for computing a date's week number. The ISO Week definitionThe 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 worksgetWeek() 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). CodeYou may copy and paste this code without charge. All we ask is you leave the credit line in the function body intact. /**
Epoch DHTML JavaScript CalendargetWeek() was developed for the Epoch DHTML JavaScript Calendar and Epoch Prime AJAX JavaScript Calendar. If you're looking for professional grade calendar components, we think you'll like them! |