week number for date/time proposal

doekman at gmail.com doekman at gmail.com
Mon Jul 23 15:56:28 PDT 2007


I noticed, there's no support for (ISO) week numbers. That is actually
quite popular in Europe. One thing of importance, is they differ from
American week numbers.

Is there time/room for improvements?

I think the implementation effort is quite moderate. I wrote the
following code a while ago (although I now would return 1-based week
numbers):
---8<-----------------------------------------------------------------------------
//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);
}
---8<-----------------------------------------------------------------------------
Thanks,
-- 
|\
| \
|_/oeke
doekman at gmail.com



More information about the Es4-discuss mailing list