February 17, 2001 - Computing the Current Work Week | WebReference

February 17, 2001 - Computing the Current Work Week

Yehuda Shiran February 17, 2001
Computing the Current Work Week
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

In many organizations, the calendar year is divided into Work Weeks, usually abbreviated as wwxy. The first week of the year is ww01, the second is ww02, and so on. The last week of the year is usually ww52 or ww53. A week starts on Sunday and ends on the following Saturday. In order to compute the current work week, we need to know how many days passed since Sunday of ww01, divide by 7, and round upwards. We can compute the number of days since Sunday of ww01 by adding the following:

date.

To compute the day of the week of January 1, we first create an instance date for January 1 (same year as today):

var newYearDayInstance = new Date(year, 0, 1); //instance of the date for January 1

And then get the day of the week:

var newYearDayOfWeek = newYearDayInstance.getDay(); //day of the week for January 1. 0 is Sunday

We then initialize the number of days passed since Sunday of ww01:

var daysUntilToday = newYearDayOfWeek; 

And then add all previous months:

for (var j = 0; j <= month - 1; j++) { //count days in all previous months
  daysUntilToday += getDays(j, year);
}

And finally, adding the number of days passed this month:

daysUntilToday += date;

We return the round-up number of the weekly count:

return Math.ceil(daysUntilToday/7);

We find the current work week, by calling getWorkWeek(); Here is the full listing of the methods:

<SCRIPT LANGUAGE="JavaScript">
function getDays(month, year) {
  // create array to hold number of days in each month
  var ar = new Array(12);
  ar[0] = 31; // January
  ar[1] = (year % 4 == 0) ? 29 : 28; // February
  ar[2] = 31; // March
  ar[3] = 30; // April
  ar[4] = 31; // May
  ar[5] = 30; // June
  ar[6] = 31; // July
  ar[7] = 31; // August
  ar[8] = 30; // September
  ar[9] = 31; // October
  ar[10] = 30; // November
  ar[11] = 31; // December
  // return number of days in the specified month (parameter)
  return ar[month];
}
function getWorkWeek() {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth();
  var date = now.getDate();
  now = null; // release memory
  var newYearDayInstance = new Date(year, 0, 1); //instance of the date for January 1
  var newYearDayOfWeek = newYearDayInstance.getDay(); //day of the week for January 1. 0 is Sunday
  var daysUntilToday = newYearDayOfWeek; //days before January 1 in ww01
  
  for (var j = 0; j <= month - 1; j++) { //count days in all previous months
    daysUntilToday += getDays(j, year);
  }
  daysUntilToday += date; //add days in current month
  return Math.ceil(daysUntilToday/7);
}
 
</SCRIPT>

Update (posted February 24, 2004)

The following information comes from Dave Matheson, a reader in Oslo, Norway.

The working week script at https://webreference.com/js/tips/010217.html works fine for US users. However the ISO standard (used in Europe) has the working week starting on a Monday. Therefore today (Sunday, Feb. 22nd 2004) would be working week 9 (in the US and per the script you have published), but week 8 in Europe (ISO standard).

To have the working week starting on a Monday (Europe / ISO standard) as opposed to Sunday (USA) the script needs to be altered as follows:

Change the variable [within the function getWorkWeek()] from:
"var date = now.getDate();" to
"var date = now.getDate()-1;"
(lies in the function "function getWorkWeek()")

This has been checked across a variety of dates and seems to work fine - though I am only a novice when it comes to Javascripts.