...

View Full Version : Resolved Changing redirect based on date



LBA1403
05-18-2012, 03:51 PM
Hi guys. I am kind of new to javascript, and was hoping that someone here could help out on a problem I have been having.

What I am trying to do is to make a page which will redirect to three diffrent sites, based on date. I want it to change to diffrent sites during the time before summer to summer, the time before xmas till xmas and from before easter till easter.
A bit more spesific this would be:

From 29. march --> 22. july (summer)
From 22. august --> 24. december (xmas)
From 3. january --> 22. march (easter)

Could you help me out? I have tried lots of diffrent ways to do this with javascripts if...else statements, but I havent yet found a way to make it work properly.

Really thankful for answers!

Philip M
05-18-2012, 04:48 PM
Here you are.



<html>
<head>
</head>
<body>

<script type = "text/javascript">

Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}

var today = new Date();
var DOY = today.getDOY()+1; // Day of Year today - 1st Jan is Day 0
var yr = today.getFullYear();

var d1 = new Date(yr, 2, 29).getDOY()+1; // 29 March
var d2 = new Date(yr, 6, 22).getDOY()+1; // 22 July
var d3 = new Date(yr, 7, 22).getDOY()+1; // 22 August
var d4 = new Date(yr, 11, 24).getDOY()+1; // 24 December
var d5 = new Date(yr, 0, 3).getDOY()+1; // 3 January
var d6 = new Date(yr, 2, 22).getDOY()+1; // 22 March

if (DOY >= d1 && DOY <=d2) {
window.location = "http://www.google.com";
}
if (DOY >= d3 && DOY <=d4) {
window.location = "http://www.javascriptkit.com";
}
if (DOY >= d5 && DOY <=d6) {
window.location = "http://www.yahoo.com";
}

</script>

</body>
</html>

You will have to change the dates for Easter each year (although it is possible to calculate Easter Sunday).
Remember that in Javascript months are 0-11.

"More than any other time in history, mankind faces a crossroads. One path leads to despair and utter hopelessness. The other, to total extinction. Let us pray we have the wisdom to choose correctly." - Woody Allen - US movie actor, comedian, & director (1935 - )

LBA1403
05-18-2012, 07:01 PM
Here you are.



<html>
<head>
</head>
<body>

<script type = "text/javascript">

Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}

var today = new Date();
var DOY = today.getDOY()+1; // Day of Year today - 1st Jan is Day 0
var yr = today.getFullYear();

var d1 = new Date(yr, 2, 29).getDOY()+1; // 29 March
var d2 = new Date(yr, 6, 22).getDOY()+1; // 22 July
var d3 = new Date(yr, 7, 22).getDOY()+1; // 22 August
var d4 = new Date(yr, 11, 24).getDOY()+1; // 24 December
var d5 = new Date(yr, 0, 3).getDOY()+1; // 3 January
var d6 = new Date(yr, 2, 22).getDOY()+1; // 22 March

if (DOY >= d1 && DOY <=d2) {
window.location = "http://www.google.com";
}
if (DOY >= d3 && DOY <=d4) {
window.location = "http://www.javascriptkit.com";
}
if (DOY >= d5 && DOY <=d6) {
window.location = "http://www.yahoo.com";
}

</script>

</body>
</html>

You will have to change the dates for Easter each year (although it is possible to calculate Easter Sunday).
Remember that in Javascript months are 0-11.

"More than any other time in history, mankind faces a crossroads. One path leads to despair and utter hopelessness. The other, to total extinction. Let us pray we have the wisdom to choose correctly." - Woody Allen - US movie actor, comedian, & director (1935 - )

Thank you very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum