blog.scoreman.net

Week number in Gantt view

I’ve already made previous post on how to add week number in the SharePoint monthly view of the calendar. This post is about how to add week numbers to the Gantt view. A customer wanted to display the headings in the Gantt view like YYWW (two digits representing the year and two digits representing the week number). This was solved using jquery and some date manipulation with javascript. The code runs on sites with English(american) regional settings but you can easily modify the script to run with other culture settings. Put the script in a content editor web part above the Gantt view or incorporate it into your master page.

Before adding the script:

After the script is added it looks like this:

Here is the script. Please change the protocol for the jquery script link depending on if you run http or https.

<script src="https://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

Date.prototype.getWeek = function() {
    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)

    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;
};

$(document).ready(function() {

    $(".ms-ganttMajorTimeUnitHeaderCell").each(
    function(intIndex) {
        var obj = $(this).attr("innerHTML");
        var firstDay = new Date(obj);

        if (firstDay.toString() != "NaN" && firstDay.toString() != "Invalid Date") {
            var week = firstDay.getWeek(firstDay.getDay());
            week = (week.toString().length == 1) ? '0' + week.toString() : week.toString();
            week = firstDay.getFullYear().toString().substr(2, 2) + week;
            $(this).attr("innerHTML", week);
        }
    });

});

</script>

One Response to “Week number in Gantt view”

  1. Michr Says:

    Hi. Great work! Can you please let me know, how to change the script so it match different Regional Settings? What in the script has to be changed?

    Thanks in advance!

Leave a Reply