When I was making contact forms for our corporate website, someone came up with this script. I simplified it greatly and now use it to fill in the week number in the contact form, as an extra bit of service. "Please contact me in week ".

The computer week goes from Sunday (day 0) to Saturday (day 6).

function getWeekNr() { var today = new Date(); Year = takeYear(today); Month = today.getMonth(); Day = today.getDate(); now = Date.UTC(Year,Month,Day+1,0,0,0); var Firstday = new Date(); Firstday.setYear(Year); Firstday.setMonth(0); Firstday.setDate(1); then = Date.UTC(Year,0,1,0,0,0); var Compensation = Firstday.getDay(); if (Compensation > 3) Compensation -= 4; else Compensation += 3; NumberOfWeek = Math.round((((now-then)/86400000)+Compensation)/7); return NumberOfWeek; } function takeYear(theDate) { x = theDate.getYear(); var y = x % 100; y += (y < 38) ? 2000 : 1900; return y; }

Now *getWeekNr()* returns the week number and I fill it in in the form:

Of course the user should be able to fill in a week number, but as a service we offer him the number of the current week.

I create an

<INPUT SIZE=2 NAME="weeknr">

and do

<BODY etc. onLoad="document.forms[0].weeknr.value=getWeekNr()">

The purpose of most of the script is to see how many days have passed since 1 Janaury of the current year.

First I need to know what date it is right now. I create a *Date* object. This is required for any date and
time calculation.

var today = new Date();

Then I see what day, month and year it is. To calculate the year, I use the function detailed on the Introduction page.

Year = takeYear(today); Month = today.getMonth(); Day = today.getDate();

Then I convert the day to Epoch Time with the *Date.UTC* method. (See the
Introduction page for more details on this method).

now = Date.UTC(Year,Month,Day+1,0,0,0);

Then I create *Firstday*, a new Date object containing the first day of the year.
(See again the
Introduction page for more details on these methods).

var Firstday = new Date(); Firstday.setYear(Year); Firstday.setMonth(0); Firstday.setDate(1);

Then I calculate the Epoch Time of 1 January CurrentYear.

then = Date.UTC(Year,0,1,0,0,0);

Then we come to the variable *Compensation*. I found out that you need
this variable and have by trial and error written the bit of script below. I first take the day number of 1 January CurrentYear
(0 = Sunday, 6 = Saturday) and then subtract 4 if the day number is larger than 3. If it isn't,
I add 3.

var Compensation = Firstday.getDay(); if (Compensation > 3) Compensation -= 4; else Compensation += 3;

The reason for this variable seems to be that the ISO has declared the week containing the first Thursday in the year to be week 1. I'm not yet entirely sure how to translate this to JavaScript, but the code above seems to work.

Then we do the real calculating. First the number of milliseconds elapsed since 1 January CurrentYear:

now-then

Convert to days by dividing it by 86,400,000 (the number of milliseconds in 1 day):

(now-then)/86400000

Add *Compensation* for whatever reason.

((now-then)/86400000)+Compensation

Divide by 7 and round, which gives NumberOfWeek. This is now returned to whatever script asked for it.

NumberOfWeek = Math.round((((now-then)/86400000)+Compensation)/7); return NumberOfWeek;

If you understand this script better than I do or if you see a mistake, please tell me what I'm doing (wrong).