# Javascript to find the weeknumber (Gregorian Calendar)

, 29 Apr 2003
 Rate this:
Been searching the Internet for a waterproof way to find weeknumber based on a certain date? Search no more, because here is the solution,

## Introduction

This short article reveals the javascript-way to get week-number based on a certain date. Week-number normally spans from week 1 (first week of a year) to week 52, but in the gregorian calendar some years have 53 weeks. I have myself looked around for a javascript to find weeknumber and use the gregorian calendar rules. But I could not find any. Giving up the search I started the work making the function by myself. I looked up the rules of the calendar, found a neat formel by Peter-Paul Koch, implemented it in javascript, and here it is for you all to use free of charge.

## Using the code

Since this code is not a complete project, rather just a snippet, there is no download. You just have to copy the code and paste it in your favourite editor. The function expects numeric values for year, month and day. In Javascript the months are 0 to 11, so the funtion expects that span of numbers representing the months (ex. january = 0 .... desember = 11). I didn't bother to write a code calling the function as I think it is pretty obvious how to use it.

Here it is:

```function getWeek(year,month,day){
//lets calc weeknumber the cruel and hard way :D
//Find JulianDay
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 jd = (day+1)+Math.Round(((153*m)+2)/5)+(365+y) +
//                 Math.round(y/4)-32083;    // (julian calendar)

//now calc weeknumber according to JD
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;
}```

Please ask in the forum below if you need help using the function.

## Points of Interest

There are rules and ways in the Gregorian calendar that is not common all around the world. I believe USA dont use this week-number method. Consider this before implementing the function.

## History

- getWeek 1.0 released

A list of licenses authors might use can be found here

## Share

Web Developer
Norway
Tommy live in Tromsø, a city far up north in Norway. He does programming and webdevelopment for a living.

 First Prev Next
 Curent month (monthString) nikiobicata 30-Jan-08 3:26
 Re: Curent month (monthString) nikiobicata 31-Jan-08 12:18
 Re: Curent month (monthString) tommy skaue 31-Jan-08 22:45
 Date kas2006 6-May-06 5:10
 Re: Date tommy skaue 7-May-06 9:43
 A better implementation Alvaro Mendez 16-Dec-05 8:52
 I found a better algorithm here: http://www.quirksmode.org/js/week.html[^] I modified it to give me the actual week number, regardless of when the first day of the year falls, and here's the result: ```function getWeekNumber(month, day, year) { var now = new Date(year, month, day + 1); var then = new Date(year, 0, 1); var firstDay = new Date(year, 0, 1); var compensation = firstDay.getDay() + 3; return Math.round((((now - then) / 86400000) + compensation) / 7); }``` Regards, Alvaro
 Re: A better implementation happy_she 6-Apr-06 5:48
 Re: A better implementation hrabe 9-May-07 15:05
 Source of formula tommy skaue 9-May-03 4:59
 Interesting facts Sire404 8-May-03 23:53
 Re: Interesting facts tommy skaue 9-May-03 0:10
 Last Visit: 31-Dec-99 19:00     Last Update: 1-Apr-15 20:06 Refresh 1