Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-18-2012, 02:51 PM   PM User | #1
LBA1403
New to the CF scene

 
Join Date: May 2012
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
LBA1403 is an unknown quantity at this point
Changing redirect based on date

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!

Last edited by LBA1403; 05-18-2012 at 06:04 PM..
LBA1403 is offline   Reply With Quote
Old 05-18-2012, 03:48 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,038
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here you are.

Code:
<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 - )
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 05-18-2012 at 04:16 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
LBA1403 (05-20-2012)
Old 05-18-2012, 06:01 PM   PM User | #3
LBA1403
New to the CF scene

 
Join Date: May 2012
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
LBA1403 is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Here you are.

Code:
<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!
LBA1403 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 PM.


Advertisement
Log in to turn off these ads.