Javascript tool for returning weeknumbers
Returns the week number according to ISO-8601, zero-based (1 is 2nd week).
//Like valueOf(), only resolution in days, not ms.
Date.prototype.valueInDays=function() {
return parseInt(new Date(this.getFullYear(),
this.getMonth(),
this.getDate()).valueOf()/(24*60*60*1000));
}
//Day of week, zero-based (monday=0, sunday=6)
Date.prototype.getWeekDay=function() {
return this.getDay()==0 ? 6 : this.getDay()-1;
}
//Week number according to ISO-8601, zero-based (1 is 2nd week).
Date.prototype.getWeekNr=function() {
var x = new Date(this.getFullYear(),0,1);
x.setDate([0,0,0,0,3,2,1][x.getWeekDay()]+x.getDate());
if(this.valueInDays() < x.valueInDays())
return new Date(this.getFullYear()-1,12-1,31).getWeekNr();
else
return parseInt((this.valueInDays()-x.valueInDays()+x.getWeekDay())/7,10);
}
|
|
Description
Usage:
alert(new Date().getWeekNr()+1).
Top