Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / Javascript
Article

Javascript to find the weeknumber (Gregorian Calendar)

Rate me:
Please Sign up or sign in to vote.
3.00/5 (8 votes)
29 Apr 20031 min read 128.8K   16   14
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:

JavaScript
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
QuestionSolution Pin
Betina Jessen17-Oct-22 14:08
Betina Jessen17-Oct-22 14:08 
QuestionPlease Create a Cut & Paste WEEKLY Redirect for Non-Profit Church Site That Will Be Useful to Many Web Developers Pin
pastormom7-Sep-08 5:47
pastormom7-Sep-08 5:47 
GeneralCurent month (monthString) Pin
nikiobicata30-Jan-08 2:26
nikiobicata30-Jan-08 2:26 
GeneralRe: Curent month (monthString) Pin
nikiobicata31-Jan-08 11:18
nikiobicata31-Jan-08 11:18 
GeneralRe: Curent month (monthString) Pin
tommy skaue31-Jan-08 21:45
tommy skaue31-Jan-08 21:45 
GeneralDate Pin
kas20066-May-06 4:10
kas20066-May-06 4:10 
GeneralRe: Date Pin
tommy skaue7-May-06 8:43
tommy skaue7-May-06 8:43 
GeneralA better implementation Pin
Alvaro Mendez16-Dec-05 7:52
Alvaro Mendez16-Dec-05 7:52 
GeneralRe: A better implementation Pin
happy_she6-Apr-06 4:48
happy_she6-Apr-06 4:48 
AnswerRe: A better implementation Pin
hrabe9-May-07 14:05
hrabe9-May-07 14:05 
GeneralSource of formula Pin
tommy skaue9-May-03 3:59
tommy skaue9-May-03 3:59 
GeneralInteresting facts Pin
Sire4048-May-03 22:53
Sire4048-May-03 22:53 
Excellent article! I didn't know there was such a clever algorithm for this.

For those who doesn't know:
This is the ISO 8601 standard, used extensively in Europe (but not in all countries!) The first week is the first week with four consecutive days, starting with monday.

The detailed logic is as follows:

A year is divided into either 52 or 53 calendar weeks.
A calendar week has 7 days. Monday is day 1, Sunday is day 7.
The first calendar week of a year is the one containing at least 4 days.
If a year is not concluded on a Sunday, either its 1-3 last days belong to next year's first calendar week or the first 1-3 days of next year belong to the present year's last calendar week.
Only a year starting or concluding on a Thursday has 53 calendar weeks.

Interesting sidenote: Microsoft Windows has had a bug in it's datepart function for ages, making wrong week calculations for some 53-week years. It was still in Windows 2000 server, I hope they've fixed it now.


No one is perfect. Welll, there was this guy... but we killed him.

GeneralRe: Interesting facts Pin
tommy skaue8-May-03 23:10
tommy skaue8-May-03 23:10 

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.