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 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Changing content based on week

    Greetings!

    What I'm looking to do is write 52 pages of content in advance, and then have index.html automatically pick the right page based on which week we are on, with the week starting on Sunday, if possible


    Can this be done, and if so, can someone help me with the code (as I am a n00b at coding)?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You need to find the start of the first week of the year, which can fall at the end of the previous year,
    and the first week of the next year, which can be the end of the current year.

    Most weeks are based on Monday, but you can use Sunday-
    Monday based weeks begin the year with first week that contains a Thursday,
    so we'll look for Wednesday instead.

    (Some years contain 53 weeks)

    I'll split it into two functions:


    Code:
    //get the first sunday of the first week of year y(or current year)
    function getWeek1(y){
    	if(y== undefined) y= new Date().getFullYear();
    	var start= new Date(y, 0, 1);
    	if(start.getDay()>3) start.setDate(start.getDate()+7);
    	while(start.getDay()!= 0) start.setDate(start.getDate()-1);
    	return start;
    }
    //get the week number of date now (or current day)
    function getWeek(now){
    	if(now== undefined) now= new Date();
    	var y= now.getFullYear(), 
    	start= getWeek1(y), 
    	end= getWeek1(y+1);
    	if(now> end) return 1; // first week of next calendar year
    	if(now<start) start= getWeek1(y-1);// last week of previous year
    	return Math.ceil((now-start)/6.048e+8);
    }
    Last edited by mrhoo; 05-17-2013 at 05:15 PM. Reason: comments

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Another method:-

    Code:
    <div id = "myweek"></div>
    <script type = "text/javascript">
    Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);  // month 0 is January
    return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
    } 
    
    var today = new Date();
    var weekno = today.getWeek(); 
    document.getElementById("myweek").innerHTML = "Week number: " + weekno;
    </script>
    
    </body>
    </html>
    This is based on the week in which January 1st falls as being Week 1, though not all of the days in that week are necessarily in the current year. A week is defined as running from Sunday - Saturday. So if January 1st is a Saturday, Week 1 has only one day. As mrwho points out, there are always 53 'weeks' (or partial weeks) in a year. But of course the pages for Week 53 and Week 1 can be the same.


    Quizmaster: Which Soviet leader resigned in 1991?
    Contestant: Lenin
    Last edited by Philip M; 05-17-2013 at 04:38 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.


  •  

    Posting Permissions

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