Week numbers are complicated, since there are different ways to calculate which week it is. The ISO week always starts on Monday. The first week of the year is either calculated by finding the first of January or the week including January 4th.

The first week of 2013 started with 31st of December this year, while it started with the 2nd of January the year before. But that’s if you follow the ISO specification as shown in the Swedish calendar below.

Figure 1: Swedish calendar

In the United States, (amongst several countries) the week starts with Sunday, and the first week of the year begins with January 1st.

Figure 2: US calendar


To get the week of the year you simply invoke:

using System.Globalization;
using System.Threading;
class Program
    static void Main(string[] args)
        var theDate = new DateTime(2012, 1, 1);
        Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
        var calendar = CultureInfo.CurrentCulture.Calendar;
        var formatRules = CultureInfo.CurrentCulture.DateTimeFormat;
        var week = calendar.GetWeekOfYear(theDate, formatRules.CalendarWeekRule, formatRules.FirstDayOfWeek);
        Console.WriteLine("SE week: " + week);
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        calendar = CultureInfo.CurrentCulture.Calendar;
        formatRules = CultureInfo.CurrentCulture.DateTimeFormat;
        week = calendar.GetWeekOfYear(theDate, formatRules.CalendarWeekRule, formatRules.FirstDayOfWeek);
        Console.WriteLine("US week: " + week);

The output follows:

Figure 3: Different week numbers for first week of year.

The first week of year bug

However, if we examine the calendar in Windows it will show the following for January 2013:

Figure 4: Week bug

By the ISO definition, the 31st of December should be part of week 1, 2013. Let’s prove that with some code:

using System.Globalization;
using System.Threading;
class Program
    static void Main(string[] args)
        var theDate = new DateTime(2013, 12, 31);
        var calendar = CultureInfo.CurrentCulture.Calendar;
        var week = calendar.GetWeekOfYear(theDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
        Console.WriteLine("ISO week: " + week);

The result follows:

Figure 5: ISO week number

That’s the only exception that I’ve found. If you try January the 6th it will show week 1, and January the 7th will show week 2.


Neither JavaScript nor Globalize has a method for getting the week number. There are several scripts out there which can do that. Here is one that I found at stackoverflow.com.

/* For a given date, get the ISO week number
    * Based on information at:
    * http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
    * Algorithm is to find nearest thursday, it's year
    * is the year of the week number. Then get weeks
    * between that date and the first day of that year.
    * Note that dates in one year can be weeks of previous
    * or next year, overlap is up to 3 days.
    * e.g. 2014/12/29 is Monday in week 1 of 2015
    * 2012/1/1 is Sunday in week 52 of 2011
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 [d.getFullYear(), weekNo];

You can use it like this:

var d = new Date();

However, I prefer to extend the date object with the method instead.

Date.prototype.getWeekNumber = function()
// the code here

Which allows us to: