A search on StackOverflow revealed plenty of ways to do this, but they all seemed to use date subtractions based on the fact that a date is represented internally as the number of milliseconds since 1 January 1970. To me that fact feels like an internal implementation detail, so I don't really like to use it. Also, I don't (want to) know how things like leap seconds would influence the reliability of these subtractions. No choice then but to reinvent the wheel and come up with my own algorithm and implementation.

The ISO 8601 standard says that week 1 of a year is the first week with a Thursday in that year. Based on that, I came up with the following "algorithm":

- Given a date d, adapt this date to Thursday of the same week.
- Subtract 1 week from this date and repeat this until the result is in the previous year.
- The week number for date d is the number of subtractions needed in the previous step to get to the previous year.

All these considerations led me to the following JavaScript function. Please let me know if there are any flaws anywhere in the function, or in the considerations.

```
function getWeekNumber(date) {
var addDays = 0, dayOfWeek = date.getDay(),
modifiedDate = new Date(date);
// move to Thursday in same week as date
if (dayOfWeek == 0) {
addDays = -3;
}
else {
addDays = 4 - dayOfWeek;
}
modifiedDate.setDate(modifiedDate.getDate() + addDays);
// count weeks going back in time one week at a time
// until we reach previous year
var year = modifiedDate.getFullYear(), weekCount = 0;
do {
modifiedDate.setDate(modifiedDate.getDate() - 7);
weekCount++;
} while (modifiedDate.getFullYear() == year);
return weekCount;
}
```

**Note:**the code assumes weeks start on Mondays. In some countries (and in JavaScript) weeks start on Sundays, so there the first

*if*statement would not be needed.

Since we're on a web page, let's test the function:

## No comments:

## Post a Comment