Question

For a system I'm working on, I need to have a "fortnight number" that works just like the standard week number, it only rolls over every 14 days instead of 7.

In other words, I'm looking for the algorithm, not the code, to calculate a week number from a date, and also the start/end dates given a year and week number.

The question comes from the fact that week 1 in any year is generally not Jan 1st, and it may even be in the previous December. There are also years that end up having 53 weeks because of this.

For example:

  • Week 1 2009 started on 2008-12-29
  • Thus 2009 had a week #53, from 2009-12-28 to 2010-01-03
  • Week 1 2010 started on 2010-01-04

How are these numbers derived?

Was it helpful?

Solution

How are these numbers derived?

Well, that depends on what system you're using, but I suspect you're after the ISO-8601 week number, which is defined like this:

2.2.10 calendar week number

ordinal number which identifies a calendar week within its calendar year according to the rule that the first calendar week of a year is that one which includes the first Thursday of that year and that the last calendar week of a calendar year is the week immediately preceding the first calendar week of the next calendar year

Bear in mind that a week in ISO-8601 starts on Monday and finishes on Sunday - so another way of expressing the "first Thursday" rule is that the first week of the year is the first week containing at least four days of the new year.

Another vital point is that when you're expressing values using "week of week year", you need to use the "week year" itself as well, not the normal year. So for Sunday January 3rd 2010, you might express this as "Week 53, week year 2009, day-of-week Sunday". It's all too easy to use the "wrong type of year" and end up messing up values around the end of December and start of January.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top