Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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 - )
    Last edited by Philip M; 05-18-2012 at 04:16 PM.

    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.

  • Users who have thanked Philip M for this post:

    LBA1403 (05-20-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •