Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2002, 08:19 PM   PM User | #1
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 593
Thanks: 1
Thanked 19 Times in 19 Posts
jalarie is an unknown quantity at this point
Get Week Number

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();        &nbsp ;             // today
  } else {
    GW_ix1=GW_Date.indexOf('/');            // find first slash
    GW_M=GW_Date.substring(0,GW_ix1);      &nbs p;// ...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);      &nbs p;// ...date
    GW_Y=GW_Work.substring(GW_ix1+1);      &nbs p;// ...year
    GW_Now=new Date(GW_Y, GW_M-1, GW_D);    // target date
  }
  GW_Now_Y=GW_Now.getYear();          &n bsp;     // 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;&nbs p;&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;&nb sp;&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=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p;(GW_Type&nbsp;==&nbsp;'MS7')&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Mon-Sun,&nbsp;first&nbsp;FULL&nbsp;week&nbsp;is&nbsp;#1
&nbsp;&nbsp;&nbsp;&nbsp;GW_Work=(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;
}

Last edited by jalarie; 10-07-2002 at 02:11 PM..
jalarie is offline   Reply With Quote
Old 10-07-2002, 11:47 PM   PM User | #2
J__C
New to the CF scene

 
Join Date: Oct 2002
Location: USA
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
J__C is an unknown quantity at this point
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).
__________________
-JC
J__C is offline   Reply With Quote
Old 10-08-2002, 01:21 PM   PM User | #3
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 593
Thanks: 1
Thanked 19 Times in 19 Posts
jalarie is an unknown quantity at this point
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 is offline   Reply With Quote
Old 10-25-2002, 02:09 PM   PM User | #4
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 593
Thanks: 1
Thanked 19 Times in 19 Posts
jalarie is an unknown quantity at this point
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.
jalarie is offline   Reply With Quote
Old 11-03-2002, 06:30 AM   PM User | #5
bcbasslet
New Coder

 
Join Date: Sep 2002
Location: New York
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
bcbasslet is an unknown quantity at this point
thanks

this one works great.
when my site is finished , i'll show you how i use it.
thanks so much.
bcbasslet is offline   Reply With Quote
Old 12-03-2002, 02:30 PM   PM User | #6
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 593
Thanks: 1
Thanked 19 Times in 19 Posts
jalarie is an unknown quantity at this point
bcbasslet, is your site up yet? I'm eager to see what you've done.
jalarie is offline   Reply With Quote
Old 12-06-2002, 02:27 AM   PM User | #7
bcbasslet
New Coder

 
Join Date: Sep 2002
Location: New York
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
bcbasslet is an unknown quantity at this point
yes its working

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.
__________________
WebFocus developer
bcbasslet is offline   Reply With Quote
Old 12-06-2002, 05:50 AM   PM User | #8
bcbasslet
New Coder

 
Join Date: Sep 2002
Location: New York
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
bcbasslet is an unknown quantity at this point
ok, the code is back on the beta site

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.

Attached Files
File Type: zip getweekfiles.zip (3.4 KB, 322 views)
__________________
WebFocus developer
bcbasslet is offline   Reply With Quote
Old 01-21-2003, 02:33 PM   PM User | #9
jalarie
Regular Coder

 
Join Date: Jun 2002
Location: Flint, Michigan, USA
Posts: 593
Thanks: 1
Thanked 19 Times in 19 Posts
jalarie is an unknown quantity at this point
bcbasslet, glad to have helped.
jalarie is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:50 PM.


Advertisement
Log in to turn off these ads.