Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Changing content based on week

1. 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)?

• 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);
}```

• 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

•

Posting Permissions

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