getISOWeek() in JavaScript

While 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 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).

Code

You may copy and paste this code without charge. All we ask is you leave the credit line in the function body intact.

/**
* Returns the week number for this date. dowOffset is the day of week the week
* "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
* the week returned is the ISO 8601 week number.
* @param int dowOffset
* @return int
*/

Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.epoch-calendar.com */

dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/

weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};

Epoch DHTML JavaScript Calendar

getWeek() 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!