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
if (GW_Now_Y < 70) { GW_Now_Y=GW_Now_Y*1+2000; }
if (GW_Now_Y < 1900) { GW_Now_Y=GW_Now_Y*1+1900; }
GW_Then=new Date(GW_Now_Y, 0, 1); // Jan 1 of current year
GW_Then_Day=GW_Then.getDay(); // Jan 1 day of the week
GW_Diff=GW_Now*1-GW_Then*1; // difference in miliseconds
GW_Days=GW_Diff/(1000*60*60*24); // ...in days
GW_Days=Math.floor(GW_Days+(1/24)); // Daylight Savings Time correction
GW_Week='*** Error';
if (GW_Type == 'SS1') { // Sun-Sat, Jan 1 in week 1
GW_Week=Math.floor((GW_Days+GW_Then_Day)/7)+1;
}
if (GW_Type == 'MS1') { // Mon-Sun, Jan 1 in week 1
GW_Week=Math.floor((GW_Days+GW_Then_Day+6)/7);
}
if (GW_Type == 'MS4') { // Mon-Sun, Jan 4 in week 1 (ISO-8601)
GW_Work=(GW_Then_Day+2)%7;
GW_Week=Math.floor((GW_Days+GW_Work+4)/7);
}
if (GW_Type == 'SS7') { // Sun-Sat, first FULL week is #1
GW_Work=(GW_Then_Day+6)%7;
GW_Week=Math.floor((GW_Days+GW_Work+1)/7);
}
if (GW_Type == 'MS7') { // Mon-Sun, first FULL week is #1
GW_Work=(GW_Then_Day+5)%7;
GW_Week=Math.floor((GW_Days+GW_Work+1)/7);
}
return GW_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
if (GW_Now_Y < 70) { GW_Now_Y=GW_Now_Y*1+2000; }
if (GW_Now_Y < 1900) { GW_Now_Y=GW_Now_Y*1+1900; }
GW_Then=new Date(GW_Now_Y, 0, 1); // Jan 1 of current year
GW_Then_Day=GW_Then.getDay(); // Jan 1 day of the week
GW_Diff=GW_Now*1-GW_Then*1; // difference in miliseconds
GW_Days=GW_Diff/(1000*60*60*24); // ...in days
GW_Days=Math.floor(GW_Days+(1/24)); // Daylight Savings Time correction
GW_Week='*** Error';
if (GW_Type == 'SS1') { // Sun-Sat, Jan 1 in week 1
GW_Week=Math.floor((GW_Days+GW_Then_Day)/7)+1;
}
if (GW_Type == 'MS1') { // Mon-Sun, Jan 1 in week 1
GW_Week=Math.floor((GW_Days+GW_Then_Day+6)/7);
}
if (GW_Type == 'MS4') { // Mon-Sun, Jan 4 in week 1 (ISO-8601)
GW_Work=(GW_Then_Day+2)%7;
GW_Week=Math.floor((GW_Days+GW_Work+4)/7);
}
if (GW_Type == 'SS7') { // Sun-Sat, first FULL week is #1
GW_Work=(GW_Then_Day+6)%7;
GW_Week=Math.floor((GW_Days+GW_Work+1)/7);
}
if (GW_Type == 'MS7') { // Mon-Sun, first FULL week is #1
GW_Work=(GW_Then_Day+5)%7;
GW_Week=Math.floor((GW_Days+GW_Work+1)/7);
}
return GW_Week;
}