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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
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:

  • Days in ww01 before January 1. This is actually the day of the week for January 1. If January 1 is 2 (Tuesday), for example, there are two days in ww01 that belong to the previous year (Sunday and Monday).
  • Days in all months prior to current month. If today is February, for example, previous months include just January.
  • Days in the current month that have passed. This is actually the current 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 http://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.


People who read this tip also read these tips:

Look for similar tips by subject:



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider