Get the week number with JavaScript – update

Hey guys,

Just wanted to let you know that I have found a better way to find the week number with javascript for a date. I pulled this gem from the datejs source:


function getWeek (getdate) {
        var a, b, c, d, e, f, g, n, s, w;
        
        $y = getdate.getFullYear();
        $m = getdate.getMonth() + 1;
        $d = getdate.getDate();

        if ($m <= 2) {
            a = $y - 1;
            b = (a / 4 | 0) - (a / 100 | 0) + (a / 400 | 0);
            c = ((a - 1) / 4 | 0) - ((a - 1) / 100 | 0) + ((a - 1) / 400 | 0);
            s = b - c;
            e = 0;
            f = $d - 1 + (31 * ($m - 1));
        } else {
            a = $y;
            b = (a / 4 | 0) - (a / 100 | 0) + (a / 400 | 0);
            c = ((a - 1) / 4 | 0) - ((a - 1) / 100 | 0) + ((a - 1) / 400 | 0);
            s = b - c;
            e = s + 1;
            f = $d + ((153 * ($m - 3) + 2) / 5) + 58 + s;
        }
        
        g = (a + b) % 7;
        d = (f + g - e) % 7;
        n = (f + 3 - d) | 0;

        if (n < 0) {
            w = 53 - ((g - s) / 5 | 0);
        } else if (n > 364 + s) {
            w = 1;
        } else {
            w = (n / 7 | 0) + 1;
        }
        
        $y = $m = $d = null;
        
        return w;
    }

weeknumber = getWeek(new Date());
									

Now you have the correct week number with JavaScript!

This entry was posted in Common. Bookmark the permalink.

10 Responses to Get the week number with JavaScript – update

  1. Pingback: Get the week number with JavaScript

  2. Juanma says:

    I found this:

    Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    return Math.ceil((((this – onejan) / 86400000)
    + onejan.getDay()+1)/7);
    }

    from here: http://javascript.about.com/library/blweekyear.htm

  3. admin says:

    Juanma: I tested your code earlier ( http://tech-hacks.net/tech/19/get-the-weeknumber-with-javascript/ )and the datejs code is more reliable than the one you provide.

  4. stojoman says:

    I have tried it with:
    getWeek(new Date(2012,11,31));
    and it returns 1, but should be 53 :)

  5. Mano says:

    How comes when I use this script there appears d/mmm/yyyy.
    I made with Acrobat Pro a form with fields. One of those fields is the date, showing d-mmm-yyyy [8-nov-2012] perfectly. Another field is the daynumber, showing 8. So also perfect.
    But I can’t to get the weeknumber working :-(
    Maybe I’m more than a newbie I thought I was :-S
    [sorry for my English]

    P.S.: I used this for the page:

    var f = this.getField(“date”);
    f.value = util.printd(“d/mmm/yyyy”, new Date());

  6. Juha says:

    stojoman on August 4, 2012 at 10:56 am said:

    I have tried it with:
    getWeek(new Date(2012,11,31));
    and it returns 1, but should be 53 :)

    <– That is wrong. Last day of that year is in a week 1

  7. Lauri says:

    It seems that there might be some javascript related rounding errors with the example code, occurring when trying to get week number for March 3rd 2015, which should return 10 but returns 9. This also happens in 2026 on week 10.

    Not too often though, but if some other functionality depends on that function it might do some funny things on such occasions, if the week is calculated based on mondays.

  8. Dre says:

    I’ve tried this code but it seems that the code assumes that the week starts on monday. But I need it to start on Sunday. What must I change?

    I could do it myself if the code was commented but unfortunately I cant seem to wrap my head around this one.

    Thanks in advance!

  9. Dre says:

    Not the most accurate of methods; but I just added 1 to the input date.

  10. Dre says:

    Oh never mind. This assigns week 1 to the week that has the first thursday. Not the 1st of the year like MS Outlook.

Leave a Reply

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