1. Computing
Send to a Friend via Email

Standard Week Number

Adding to the Date Object

In my getWeek script I took the easy way out for the processing and assumed that the week runs from Sunday to Saturday (as the javaScript numbering of the days of the week works it) with the first of January in week one even if that week ended up having only one day. The International Standards Organisation (ISO) have their own definition for week numbering where the week is considered to run from Monday to Sunday, week one has at least four days from the current year in it, and up to three days on the start or end of the year is effectively transferred to the adjacent year in order for each week to have exactly seven days. Leaving aside any reasons why JavaScript and the ISO consider the week to start on either Sunday or Monday when the days of the week themselves are named starting from Saturday, let's look at modifying the getWeek script to allow us to obtain the week number in accordance with the ISO standard.

In order to do this we are going to need three methods to replace the getWeek() method from the earlier script.

The first of the extra methods that we will need is one that will get the day of the week based on Monday instead of Sunday as the first day. We can then substitute this into our getWeek method in order to shift the week calculations so that the week runs from Monday to Sunday instead of from Sunday to Saturday.

The second of the extra methods we need is one that determines which year that the week the current date is in. For 51 weeks of the year this will be the same as the actual year but for the first three and last three das of the year the week that the date is in may in fact be considered to belong to the adjacent year. The easiest way to work this out is to find the closest Thursday to the current date and see what year that day is in since with a week running from Monday to Sunday and the week belonging to whichever year has the most days in it Thursday will always be a date in the right year since whichever year that Thursday is in has at least four days from that real year in the current week and the week therefore is considered to belong to that year.

Substituting these two new methods for the getFullYear() and getDay() calls in the original getWeek script almost gives us the right answer. It takes care of the changes for which days the week starts and finishes on and it takes care of making sure that the week number is calculated based on which year the week belongs to. The only thing we still need to adjust for is that our script still assumes that 1st January is in week one which may or may not be true depending on which day of the week it falls on. The final adjustment that we need is to subtract one from the week number if the 1st January ends up being in the last wek of the preceding year (which is the case if it falls on a Friday, Saturday, or Sunday).

This gives us some fairly simple code that will give us the official week number that any date belongs to in accordance with the ISO standards and at the same time provides us with a way to know what year that week belongs to for the few days at the start or end of years that are considered to be in the first or last week of a different year from the actual year to which the date belongs.

Here's the code that does all this:

Date.prototype.getMDay = function() {
return (this.getDay() + 6) %7;}
Date.prototype.getISOYear = function() {
var thu = new Date(this.getFullYear(), this.getMonth(), this.getDate()+3-this.getMDay());
return thu.getFullYear();}
Date.prototype.getISOWeek = function() {
var onejan = new Date(this.getISOYear(),0,1);
var wk = Math.ceil((((this - onejan) / 86400000) + onejan.getMDay()+1)/7);
if (onejan.getMDay() > 3) wk--;return wk;}

For an example of how to use this code let's call it using today's date using:

var today = new Date();
document.getElementById('week').innerHTML = today.getISOWeek();
document.getElementById('year').innerHTML = today.getISOYear();

Provided that those ids are included in appropriate span tags as they are in this sentence we can then have our page tell everyone that today is officially in week unknown of unknown.

©2014 About.com. All rights reserved.