Week numbers in FullCalendar

To add week numbers in FullCalendar, use the following code:

var weekNumberPrototype = $("<span class=\"week-number\"></span>").css({"float": "left", "fontStyle": "italic", "fontSize": "10px", "paddingLeft": "3px"});
var weekNumbers;

// ... here comes init code of FullCalendar ...
  viewDisplay: function(view){
    if($("#mycal span.week-number").length == 0){
      weekNumbers = $("#mycal div.fc-view-month > table tr td:first-child > div:first-child").prepend(weekNumberPrototype).find("span.week-number");
    }
    
    var week = getWeekNumber(view.start);
    
    for(var i = 0; i < weekNumbers.length; i++){
      weekNumbers.eq(i).text(week + i).attr("title", "Week " + (week + i));
    }
  }
// ...

// following code by merlyn et al:

function getWeekNumber(d) {
  // Copy date so don't modify original
  d = new Date(d);
  d.setHours(0,0,0);
  // Set to nearest Thursday: current date + 4 - current day number
  // Make Sunday's day number 7
  d.setDate(d.getDate() + 4 - (d.getDay()||7));
  // Get first day of year
  var yearStart = new Date(d.getFullYear(),0,1);
  // Calculate full weeks to nearest Thursday
  var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
  // Return array of year and week number
  return weekNo;
}