blog.scoreman.net

Week numbers in calendar view – revisited for SharePoint 2010

A while ago I wrote a post on how to add week numbers to the monthly calendar view. The customer I built this for is now upgrading to SharePoint 2010 and since the calendar is now completely rebuild with ajax functionality I had to revisit and rewrite this code. This time I’m doing it with jQuery instead which in my opinion leads to a much cleaner solution. Thanks to Mike Smith’s post for showing how to intercept the asynchronous ajax events  of the calendar. The neet thing about this is that even if asynchronous events happen like if you page to a new month, the calendar still gets updated with new week numbers. For calculating week number I’m using the same javascript as in my post on how to add week numbers to the gantt view.

Week Numbers in Calendar

This is the code you need to put in a content editor web part on the same page as the calendar is displayed.

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

Date.prototype.getWeek = function () {
   //lets calc weeknumber the cruel and hard way 😀
   //Find JulianDay
   var year = this.getFullYear();
   var month = this.getMonth();
   var day = this.getDate();

   month += 1; //use 1-12
   var a = Math.floor((14 - (month)) / 12);
   var y = year + 4800 - a;
   var m = (month) + (12 * a) - 3;
   var jd = day + Math.floor(((153 * m) + 2) / 5) +
                (365 * y) + Math.floor(y / 4) - Math.floor(y / 100) +
                Math.floor(y / 400) - 32045;      // (gregorian calendar)

   //now calc weeknumber according to JD
   var d4 = (jd + 31741 - (jd % 7)) % 146097 % 36524 % 1461;
   var L = Math.floor(d4 / 1460);
   var d1 = ((d4 - L) % 365) + L;
   NumberOfWeek = Math.floor(d1 / 7) + 1;
   return NumberOfWeek;
};

function updateCampaignCalendar() {

   // calendar rows
   jQuery('TH[evtid="week"]').each(function () {

      // add week numbers
      var firstDay = new Date(jQuery(this).attr('date'));
      if (firstDay.toString() != "NaN" && firstDay.toString() != "Invalid Date") {
         var week = firstDay.getWeek(firstDay.getDay());
         jQuery(this).children("DIV").attr("innerHTML", week);
      }
   });
}

_spBodyOnLoadFunctionNames.push('calendarEventLinkIntercept');

// hook into the existing SharePoint calendar load function
function calendarEventLinkIntercept() {
   var OldCalendarNotify4a = SP.UI.ApplicationPages.CalendarNotify.$4a;
   SP.UI.ApplicationPages.CalendarNotify.$4a = function () {
      OldCalendarNotify4a();
      updateCampaignCalendar();
   }
}
</script>

Tags: , ,

7 Responses to “Week numbers in calendar view – revisited for SharePoint 2010”

  1. Hägge Says:

    Hello,

    It works fine in SP2010 on English Calenders but NOT i a Swedish Kalender.??

    Do you know a way for making it works on other languages too?

    Regards

    /Henrik

  2. admin Says:

    To get it to work with for example a Swedish calendar you would have to replace the getWeek() function with one that works for the Swedish date format. A better solution would be to get the locale from the current web and have a more generic solution to parsing the date and calculating the week number. Remember that week numbers are calculated differently in many parts of the world.

  3. Hägge Says:

    Ok, thanks for answer!

    I did not get it to work in swedish, do anyone have an working example?

    /Henrik

  4. Dave Says:

    This does not work for me. When I add the code, nothing even happens.

    I have tried downloading the js file from your page and uploading it into my site, then referencing that in the content editor. No luck. I have also tried just using the same exact code copy/pasted into my content editor. No luck.

    Any ideas?
    Code work?

    Thanks

  5. Shivaji Says:

    I have tried this on my Calendar then its observed that

    with http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js files it works perfectly but with

    http://code.jquery.com/jquery-1.7.2.min.js it does not works.

    Any reason for this ?

  6. erik Says:

    It worked with an extra semicolon in the last part
    but after saving the page the code in the content editor webpart dissapeared.
    ***************
    updateCampaignCalendar();
    };
    }

  7. erik Says:

    works on sp2010
    using txt file and referencing it within the content editor webpart

    didn’t work on office 365 though
    Any idea?
    Thanks

Leave a Reply