Get the week number with JavaScript

Update: I have found a better way to get the week number with JavaScript here.

A few days ago I needed to code a little calendar in JavaScript. While I was doing this, I needed a piece of code to get the week number of a date.

If you, for example, need to get the day of the month with JavaScript you can use this:

var mydate = new Date();
month = mydate.getMonth();
									


I wanted to get a ISO 8601 week number with the same method. You can use this piece of code to get this actually working:

Date.prototype.getWeek = function() {
    var determinedate = new Date();
    determinedate.setFullYear(this.getFullYear(), this.getMonth(), this.getDate());
    var D = determinedate.getDay();
    if(D == 0) D = 7;
    determinedate.setDate(determinedate.getDate() + (4 - D));
    var YN = determinedate.getFullYear();
    var ZBDoCY = Math.floor((determinedate.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
    var WN = 1 + Math.floor(ZBDoCY / 7);
    return WN;
}
									



Example – Get the week number of the current day:

var mydate = new Date();
var weeknumber = mydate.getWeek();
									


Example – Get the week number of 2 May 2008:

var 2may2008 = new Date();
2may2008.setFullYear(2008, 4, 2);
var weeknumber = 2may2008.getWeek();

									


I tested the above method on IE 6+ and FF 2 and it works perfectly.

This entry was posted in Tech. Bookmark the permalink.

13 Responses to Get the week number with JavaScript

  1. vde says:

    Hi.

    I tried your script but I found a bug.

    The 2009-12-31 return 53th week,
    but 2010-01-01 return 01 week against 53th week.

    The transition between 2 years must test the number of day of the first week.
    If 2010-01-01 >= friday then first week begin the next monday.

  2. Andrey says:

    Very useful function. Thank you.

  3. Praveen says:

    Thanks, this function is very useful

  4. Jimbo says:

    VDE is correct, the last week of 2009 and first week of 2010 are technically the same week number (if im not mistaken, that would be week 53) And week 1 of 2010 should start on 3 Jan 2010

  5. NielsNL says:

    Thankyou very much needed this badly

  6. Monty says:

    It doesnt work, for the date of 9/4/2010 it gives 35, its 36….

  7. Lasse Ebert says:

    Thanks!

  8. ray says:

    I been looking for a weekly script. What is the code to display the current week on the page. document.write?

    thx

  9. Julien de Prabère says:

    I propose this function, which takes into account the summer(daylight saving) time !

    Date.prototype.getWeek = function() {var i,d,firstWeekMonday;
    var i=1;while (new Date(this.getFullYear(),0,i).getDay()!=4) i++;// i the first Thursday;
    var firstWeekMonday = new Date(this.getFullYear(),0,i-3,0,0,0,-1);// one millisecond before
    d=Math.ceil(((this.valueOf()-firstWeekMonday.valueOf()-(this.getTimezoneOffset()-firstWeekMonday.getTimezoneOffset())*60000)/86400000)/7);
    return d?d:new Date(this.getFullYear()-1,11,31).getWeek();
    }

  10. Julien de Prabère says:

    After cheks, your program does not need a correction for summer time but is wrong, for example in 2010, with one week too much all year long…

  11. Akbar Mahrat says:

    reduce month by 1.

  12. admin says:

    Hey everyone,

    Look at the updated code to get the weeknumber here: http://tech-hacks.net/common/72/get-the-week-number-with-javascript-update/

    The above code is more reliable.

Leave a Reply

Your email address will not be published. Required fields are marked *