Click to See Complete Forum and Search --> : Redirection based on week number


myeyesglazeover
10-01-2006, 08:33 AM
Hello,
I'm trying to create a redirect link based upon the week number of the year. I found some code that appears to do the trick but, it returns incorrect results. Here is the code:

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function showWeek() {
var today = new Date(year,month,day);
var year = y2k(today.getYear());
var month = today.getMonth();
var day = today.getDate();
return 'week' + getWeek(year,month,day) + '.htm';
}

function getWeek(year,month,day) {
var newYear = new Date(year,0,1);
var offset = 7 + 2 - newYear.getDay();
if (offset == 8) offset = 1;
if (offset == 9) offset = 2;
var daynum = ((Date.UTC(y2k(year),month,day,0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
var weeknum = Math.floor((daynum-offset+7)/7);
if (weeknum == 0) {
year--;
var prevNewYear = new Date(year,0,1);
var prevOffset = 7 + 2 - prevNewYear.getDay();
if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
}
return weeknum;
}
//-->
</SCRIPT>

<a href="weekxx.htm" onClick="this.href=showWeek()">Page of the Week</a>


Instead of returning week"weeknum".htm, it returns week"NaN".htm. I assume that it considers Monday to be the beginning of the week but, I'm not sure since it doesn't work. I tried to document.write the weeknum variable and got nothing. I'm fairly new at Javascript and it would help my understanding to find if this is a syntax or logic issue.

Any help is greatly appreciated!!!

myeyesglazeover

slaughters
10-01-2006, 09:34 AM
This should work (I even through in some comments :) ):
<html>
<head>
<script>

function GoWeekNum() {

// Get Todays current week number
WeekNumber = getWeekNum();

// Use number to build an URL then go there
WeekNumURL = "week" + WeekNumber + ".htm";
window.location = WeekNumURL;
}

function getWeekNum() {

// Get todays year, month and day number from a date object
// and use them to create a UTC date object
var today = new Date();
Year = 1900 + today.getYear();
Month = today.getMonth();
Day = today.getDate();
now = Date.UTC(Year,Month,Day+1,0,0,0);

// Create a date object for the first day of this year
// and use it to create a UTC date object
var Firstday = new Date();
Firstday.setYear(Year);
Firstday.setMonth(0);
Firstday.setDate(1);
then = Date.UTC(Year,0,1,0,0,0);

// Calculate some offset if the first day of the year
// fell before mid week or after
var offset = Firstday.getDay();
if (offset > 3) offset -= 4; else offset += 3;

// Do Calculation and return the number of the week
NumbOfWeek = Math.round((((now-then)/86400000)+offset)/7);
return NumOfWeek;
}
</script>
</head>

<body>
<a href="javascript:void(0);" onClick="GoWeekNum();">Page of the Week</a>
</body>
</html>

mrhoo
10-01-2006, 12:46 PM
By default this method begins the week on Sunday, but you can pass an
argument (1) to make weeks start on Monday.

You could make it a method of the Date.prototype to get the week number of any Date object:

Date.prototype.weeknum=function(n){
if(!n)n=0;
var d1=this;
var d2=new Date(d1.getFullYear(),0,1);
var doffset= 7- d2.getDay()+ n;
var d3= ((d1-d2)/86400000) +doffset;
return Math.floor(d3/7);
}
var d=new Date().weeknum() returns the current week number;
var d=new Date(2006,11,25).weeknum(1); returns the week number of Christmas 2006, starting weeks on Monday.

myeyesglazeover
10-01-2006, 04:56 PM
I tried the script you provided in my page and as is, clicking the link does nothing!