Click here to Skip to main content
13,250,453 members (58,935 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked
Posted 7 Mar 2008

Adding week numbers to ASP.NET Calendar Control

, 7 Mar 2008
Rate this:
Please Sign up or sign in to vote.
How to add a week number column to the calendar control using javascript


This is a simple way to add an extra column to the calendar control that shows the
week number for each row like illustrated below.



In some contries week numbers is often used and the standard calendar control does not
include the option to show week numbers.

After having experimented with different events of the control I discovered that using javascript
the column could be added very simpel, all though I would have preffered a code-behind

In my search for a solution I found many request for this, but no sutable solution.

Using the code

The code is simple. I use a javascript function "addWkColumn" to manipulate the table that is
created by the calendar control.

function addWkColumn(tblId, wkStart)
    var tbl = document.getElementById(tblId);
    var tblBodyObj = tbl.tBodies[0];
    for (var i=0; i<tblBodyObj.rows.length; i++) 
        // Month Header
        if (i==0)
            // Add extra colspan column
        // Week Header
        if (i==1)
            // Add week column headline
            var newCell = tblBodyObj.rows[i].insertCell(0);
            newCell.innerHTML = 'wk';
   = '#ffffee';
        // Normal row
        if (i >= 2 )
            // Add the weeknumbers 
            var newCell = tblBodyObj.rows[i].insertCell(0);
            newCell.innerHTML = wkStart;
            wkStart += 1;
   = '#ffffee';

Now all you have to do is to call the javascript function with the table id for the calendar table.

To do this I use the ClientScript.RegisterStartupScript from code behind, to ensure the correct
clientid name and to find the week number for the first week in the shown month.

private void addWeekNumberColumn()
    // Get the date shown in the calendar control
    DateTime curMonth = Calendar1.VisibleDate;

    // Find first day of the current month
    curMonth = Convert.ToDateTime(curMonth.Year.ToString() + "-" + curMonth.Month.ToString() + "-01");
    // Build javascript
    string jscript = "<script type='text/javascript'> addWkColumn('" + Calendar1.ClientID + "', " + getISOWeek(curMonth).ToString() + ");</script>";
     // Add script to page for execution of addWkColumn javascript function
    Page.ClientScript.RegisterStartupScript(this.GetType(), "AddWeeknumbers", jscript);
// Helper function to find ISO week
private int getISOWeek(DateTime day)
    return System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(day, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

Now all you have to do is to call the addWeekNumberColumn() function from the page_load
event of the page.

Points of Interest

Please note that the javascript function should only be called with a reference to a calendar
control created tableID, as it references cell numbers directly. This is no problem unless you call
it with a different table.

Happy coding!


Version 1.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer TimeMap ApS
Denmark Denmark
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThanks for the example... Pin
simpa6-May-11 7:30
membersimpa6-May-11 7:30 
GeneralInstead of inserting a new column Pin
Tattarn23-Jul-08 2:55
memberTattarn23-Jul-08 2:55 
GeneralRe: Instead of inserting a new column Pin
nilsreuvers2-Sep-08 3:37
membernilsreuvers2-Sep-08 3:37 
GeneralRe: Instead of inserting a new column Pin
nilsreuvers2-Sep-08 4:28
membernilsreuvers2-Sep-08 4:28 
GeneralRe: Instead of inserting a new column Pin
woodz19-Dec-08 10:28
memberwoodz19-Dec-08 10:28 
GeneralRe: Instead of inserting a new column Pin
woodz19-Dec-08 10:32
memberwoodz19-Dec-08 10:32 
QuestionIs it working properly? Pin
ebbehundborg28-Mar-08 0:37
memberebbehundborg28-Mar-08 0:37 
Very cool - thanks. I don't know if I'm using your stuff right, though. But it shows different week numbers for the same date. When my calendar is displaying August 2008 it shows the first week of September as the bottom most row having September 1st in the first column. Your code displays the week number 36 (which is correct by my paper calendar). But when I skip to the next month (via the links in the calendar header) September 1st is now in week 37. Can you help.

AnswerRe: Is it working properly? Pin
TDunk28-Mar-08 1:13
memberTDunk28-Mar-08 1:13 
GeneralISO week of the year numbers Pin
PIEBALDconsult7-Mar-08 6:13
memberPIEBALDconsult7-Mar-08 6:13 
GeneralRe: ISO week of the year numbers Pin
TDunk7-Mar-08 8:19
memberTDunk7-Mar-08 8:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 7 Mar 2008
Article Copyright 2008 by TDunk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid