View Full Version : Is there a getWeek method?
I would like to add an "Item of the Week" product feature to my site. Is there a method (maybe getWeek) that will detect what week out of 52 it is in the year and display the current week's html file?
Example: It is the 32nd week of the year. Display 32.html.
Any help would be appreciated.
-JC
ConfusedOfLife
10-02-2002, 10:36 PM
I don't think so!
If getDay showed us the number of days passed from the begining of month, then we could have written this getWeek ourselfves by the help of getMonth & getDay, but now that it doesn't, I have to think about it more!
joh6nn
10-03-2002, 02:34 AM
there's no get week function
you'd have to figure out what the first day of the year is. then, you'd have to figure out, from that, what the first sunday of the year is. then, you figure out how many weeks in each month, and how many of those months have passed. then you figure out what day today is, and how many weeks have passed since the beginning of this month, to this day.
there's no shorter way to do this, than i can think of.
adios
10-03-2002, 02:47 AM
http://developer.irt.org/script/914.htm
jalarie
10-04-2002, 03:16 PM
Please note that some people view the week as starting on Sunday while others view it as starting on Monday. Some people count FULL weeks only, so this year's week number 1 began on January 6th, while others count January 1st as being in the first week even if it is not a full 7-day week. Yet others use the ISO-8601 week number which runs Monday through Sunday and counts January 4th as being in week number 1 no matter where that puts January 1st, 2nd, and 3rd. And, no matter how you do it, you can wind up with 53 week numbers if the calendar dates fall just right; the year 2000 began on a Saturday and ended on a Sunday with days in 53 separate weeks as viewed on most calendars.
Specify exactly how you view the week number, and someone will probably write it for you.
jalarie
10-04-2002, 03:50 PM
If you like weeks that start on Sunday, and you like January 1st to always be in week number 1, then:
Now=new Date();
Now_Y=Now.getYear();
if (Now_Y < 70) { Now_Y=Now_Y*1+2000; }
if (Now_Y < 1900) { Now_Y=Now_Y*1+1900; }
Then=new Date(Now_Y, 0, 1);
Then_Day=Then.getDay();
Diff=Now*1-Then*1;
Days=Math.floor(Diff/(1000*60*60*24)+(1/24));
Week=Math.floor((Days+Then_Day)/7)+1;
alert(Week);
I appreciate the input.
I would like to start the week with Monday and end on Sunday.
That would be very helpful.
jalarie
10-04-2002, 04:29 PM
:D Monday-Sunday, January 1st always in week 1:
Now=new Date();
Now_Y=Now.getYear();
if (Now_Y < 70) { Now_Y=Now_Y*1+2000; }
if (Now_Y < 1900) { Now_Y=Now_Y*1+1900; }
Then=new Date(Now_Y, 0, 1);
Then_Day=Then.getDay();
Diff=Now*1-Then*1;
Days=Math.floor(Diff/(1000*60*60*24)+(1/24));
Week=Math.floor((Days+Then_Day+6)/7);
alert(Week);
jalarie
10-04-2002, 08:23 PM
You might like the "Get Week Number" function that I just posted in the "Post a JavaScript" area. It will do today's date or a specified date, and do most of the variations that I mentioned above. It does not do the "full weeks only" choice.
adios
10-04-2002, 09:58 PM
You might like the link someone else posted (for some reason) a while back.
I wouldn't mind seeing it. Where would it be?
Jalarie,
Using your script, I was thinking of adding this script to it to call the getWeek method:
<SCRIPT LANGUAGE="JavaScript">
<!--
function weekPage() {
today = new Date();
m = new Array(
"week01.html","week02.html",
"week03.html", etc, etc.
);
window.location = m[today.getWeek()];
}
// End -->
</script>
<BODY OnLoad="weekPage()">
What do you think?
adios
10-04-2002, 10:37 PM
Scroll (up). ;)
Adios,
Gotcha.
Thanks again.
joh6nn
10-04-2002, 10:59 PM
maybe i'm just weird, but why do both of those scripts use getYear, and manually fix the Y2K problem, instead of using getFullYear, and just getting a 4 digit year to begin with? is it stylish to do things the hard way?
whammy
10-05-2002, 01:22 AM
I know I am personally using getFullYear() from now on... if someone has a browser that doesn't support it at THIS point - that's just too bad. They must be continually seeing errors whenever they surf the net anyway... so I'm sure it won't come as a big shock. :)
ConfusedOfLife
10-05-2002, 02:04 PM
Hi Jalarie, to be frank I couldn't understand your Y2K fix that much, as a note, if NOW_Y be less than 70, then it's surely less than 1900, so, what we get is 3900, do you think that it's right?
joh6nn
10-05-2002, 09:46 PM
if the year returned is less than 70, we can safely assume that the year is over the 2000 millenial mark, so we add 2000 to it, and get the right year. once we've done that, the year is no longer less than 1900, so the next if doesn't apply.
getFullYear is still a better solution, though.
jalarie
10-07-2002, 01:08 PM
I use getYear and fix it because it works. I wish everyone used the most up-to-date browser, but they don't and I like my coding to work no matter what. Personal preference.
jalarie
10-07-2002, 02:17 PM
The getweek function in "Post a JavaScript" now handles a specified date more nicely and can count FULL weeks only if you wish. :D
bcbasslet
11-01-2002, 04:39 AM
<SCRIPT Language="JavaScript">
<!--
// Week of the year script - By Ada Shimar (ada@chalktv.com)
// Based on code by Duncan Kabinu, Florida Department of Agriculture.
// Modified by Italian anonimous on 19.08.2002
// Visit JavaScript Kit (http://javascriptkit.com) for this script and 400+ more
function weekNo() {
var totalDays = 0;
now = new Date();
years=now.getYear();
var days = new Array(12); // Array to hold the total days in a month
days[0] = 31;
days[2] = 31;
days[3] = 30;
days[4] = 31;
days[5] = 30;
days[6] = 31;
days[7] = 31;
days[8] = 30;
days[9] = 31;
days[10] = 30;
days[11] = 31;
// Check to see if this is a leap year
if (Math.round(now.getYear()/4) == now.getYear()/4) {
days[1] = 29
}else{
days[1] = 28
}
// If this is January no need for any fancy calculation otherwise figure out the
// total number of days to date and then determine what week
if (now.getMonth() == 0) {
totalDays = totalDays + now.getDate();
}else{
var curMonth = now.getMonth();
for (var count = 1; count <= curMonth; count++) {
totalDays = totalDays + days[count - 1];
}
totalDays = totalDays + now.getDate();
}
// Here is the modification: considering when start the 1st week of year.
// Originally was only: var week = Math.round(totalDays/7)
// Check if browser is "Microsoft Internet Explorer" or not and apply the right var
var agt=navigator.userAgent.toLowerCase();
if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) {
var firstday=new Date("01/01/"+String(now.getYear())).getDay();
}
else {
var firstday=new Date("01/01/"+String(1900+now.getYear())).getDay();
}
var diff=7-firstday+1;
var week = Math.round((totalDays+diff-firstday)/7); return week;
}
document.write("Welcome to <b>week "+weekNo()+"<\/b> of "+years+"!")
// -->
</SCRIPT>
<p><font face="arial" size="-2">This free script provided by</font><br>
<font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
Kit</a></font></p>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.