...

Get Week Number

jalarie
10-04-2002, 08:19 PM
The following will give you the week number for the current date or for a specified date. It can do weeks beginning on Sunday with January 1st always in week 1, starting on Monday with January 1st always in week 1, starting on Monday with January 4th always in week 1 (ISO-8601), or starting on Sunday or Monday starting with the first FULL week:

function GetWeek(GW_Type,GW_Date) {
  //  GW_Types:
  //    SS1  Sunday-Saturday, Jan 1 always in week 1.
  //    MS1  Monday-Sunday, Jan 1 always in week 1.
  //    MS4  Monday-Sunday, Jan 4 always in week 1 (ISO-8601).
  //    SS7  Sunday-Saturday, first FULL week is week 1.
  //    MS7  Monday-Sunday, first FULL week is week 1.
  //  GW_Date:
  //    Optional current date formatted as MM/DD/YYYY.  Jan-Dec=1-12.
  GW_Type=GW_Type.toUpperCase();
  if (typeof(GW_Date) == 'undefined') {
    GW_Now=new Date();                      // today
  } else {
    GW_ix1=GW_Date.indexOf('/');            // find first slash
    GW_M=GW_Date.substring(0,GW_ix1);       // ...get the month
    GW_Work=GW_Date.substring(GW_ix1+1);    // ...rest of item
    GW_ix1=GW_Work.indexOf('/');            // second slash
    GW_D=GW_Work.substring(0,GW_ix1);       // ...date
    GW_Y=GW_Work.substring(GW_ix1+1);       // ...year
    GW_Now=new Date(GW_Y, GW_M-1, GW_D);    // target date
  }
  GW_Now_Y=GW_Now.getYear();                // current year
&nbsp;&nbsp;if&nbsp;(GW_Now_Y&nbsp;<&nbsp;70)&nbsp;&nbsp;&nbsp;{&nbsp;GW_Now_Y=GW_Now_Y*1+2000;&nbsp;}
&nbsp;&nbsp;if&nbsp;(GW_Now_Y&nbsp;<&nbsp;1900)&nbsp;{&nbsp;GW_Now_Y=GW_Now_Y*1+1900;&nbsp;}
&nbsp;&nbsp;GW_Then=new&nbsp;Date(GW_Now_Y,&nbsp;0,&nbsp;1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Jan&nbsp;1&nbsp;of&nbsp;current&nbsp;year
&nbsp;&nbsp;GW_Then_Day=GW_Then.getDay();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Jan&nbsp;1&nbsp;day&nbsp;of&nbsp;the&nbsp;week
&nbsp;&nbsp;GW_Diff=GW_Now*1-GW_Then*1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;difference&nbsp;in&nbsp;miliseconds
&nbsp;&nbsp;GW_Days=GW_Diff/(1000*60*60*24);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;...in&nbsp;days
&nbsp;&nbsp;GW_Days=Math.floor(GW_Days+(1/24));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Daylight&nbsp;Savings&nbsp;Time&nbsp;correction
&nbsp;&nbsp;GW_Week='***&nbsp;Error';
&nbsp;&nbsp;if&nbsp;(GW_Type&nbsp;==&nbsp;'SS1')&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Sun-Sat,&nbsp;Jan&nbsp;1&nbsp;in&nbsp;week&nbsp;1
&nbsp;&nbsp;&nbsp;&nbsp;GW_Week=Math.floor((GW_Days+GW_Then_Day)/7)+1;
&nbsp;&nbsp;}
&nbsp;&nbsp;if&nbsp;(GW_Type&nbsp;==&nbsp;'MS1')&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Mon-Sun,&nbsp;Jan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=(GW_Then_Day+5)%7;
&nbsp;&nbsp;&nbsp;&nbsp;GW_Week=Math.floor((GW_Days+GW_Work+1)/7);
&nbsp;&nbsp;}
&nbsp;&nbsp;return&nbsp;GW_Week;
}

J__C
10-07-2002, 11:47 PM
OK. I want to use this, or some of this, but it looks a little overwhelming. I just want to display the correct html file for the correct week (Monday to Sunday).

jalarie
10-08-2002, 01:21 PM
J__C, Save the function as-is. I'll make the assumption that you want January 1st to be in the first week even if it is not a full week. Call the function like this:

&nbsp;Week=GetWeek('ms1');

Use "Week" as part of the name of the html file that you wish to display.

If you need more help, please feel free to contact me directly at:

&nbsp;jalarie@umich.edu

jalarie
10-25-2002, 02:09 PM
For whatever it's worth: I recently acquired a Palm PC and found that the week number that it reports is the ISO-8601 standard. :rolleyes:

bcbasslet
11-03-2002, 06:30 AM
this one works great.
when my site is finished , i'll show you how i use it.
thanks so much.

jalarie
12-03-2002, 02:30 PM
bcbasslet, is your site up yet? I'm eager to see what you've done.

bcbasslet
12-06-2002, 02:27 AM
but i had to fire my client , 2.p.i.t.a.
i'll repost all the site to my beta server
as soon as i get my zipdrive at the office
it will be www.stroudsburgfoto.com/susan

I'll try to get it done by the end of the weekend
the code is so swell!!! what fun it was to do, with your help
too bad the client wasn't.:)

bcbasslet
12-06-2002, 05:50 AM
the script getweek.js is here
both the js and the code for the frames where i use it
is in the attached file.
and the site in operation (for a short while) is
www.stroudsburgfoto.com/susan
the main frame of that page figures out what week it is and loads in the appropriate filename for the current week.
THANKS ALL OF YOU FOR ALL YOUR HELP.

:thumbsup:

jalarie
01-21-2003, 02:33 PM
bcbasslet, glad to have helped.
:D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum