How Difficult are WEEK NUMBERS?

Having written a number of online calendar and booking systems I have been asked to include Week Numbers on a number of occasions. It’s no big deal, as most modern languages have Date/Time support classes and they will give me the Week Number via a function call.

That was until a customer told me that my Week Numbers don’t match up with theirs. To some manufacturing companies, giving a wrong week number is equivalent to giving the wrong date – and that can lead to dire consequences.

What’s the problem?

The problem is one of definition. How do you define the the start of the year? I see it that you need to agree a number of terms:

What is the first day of the week? Before defining the first day of the week you need to decide on what day a week starts on.  When I was at school you got a pat on the head for knowing Sunday was the first day of the week. However, it seems that many businesses prefer Monday to be the first day.

How many days in a week. This may seem a strange question to ask, but if we have 7 day weeks we have 7×52 = 364 days, where a year is 365 or 366 days. What happens to the difference? We have several choices: we could allow a short week at the end of the year; we could allow one or two 8-day weeks; or we could define a week having always 7 days in which case some years will have 53 weeks.

I think that most people go for full 7-day weeks and accept that a year may have a Week 53. That means that the previous year will affect how the current year starts. So how do we define what is Week 1? There are several possible solutions for that.

We could define the first week of the year as the week that contains January 1st. That would work but if this is the last day of the week then we have the last six days of December in week 1. We would have the same problem if  we decide to use the week with the (say) first Monday of the year.

We may decide that the first week is the one that contains more January dates than December dates. Again this will work, but we may find January 1st is actually in the last week (52 or 53) of the previous year.

Of course, any of these would work but the point is that we need to be consistent and have a standard definition.

Enter the ISO

As the World got smaller and global trading began to be very common, the International Standards Organisation (ISO) got involved and in 1988 published ISO 8601 defining an international accepted way to represent date and time format.

ISO 8601 defines the following concerning week numbers:

  • Weeks always have 7 days.
  • The first day of the week is Monday
  • The first week of the year is the one that contains the first Thursday in it.

So the 4th January is always in the first week of the year.
Week 1 starts between 29th December and 4th January.
The 28th December is always in the last week of the year.
The is no Week 00.
A year ends with Week 52 or Week 53.
If you count the Thursdays it always gives the week number. For example the 20th Thursday of the year will be in Week 20.
ISO would write Week 24 of 2014 as ‘2014-W24’ or ‘2014W2’.

After all that…

Even after all that there are still major differences between calculations and not everyone uses the ISO definition.

North America still tend to use Sunday as the first day of the week, and the first week is the one containing the first Saturday so will always contain the 1st January.

The Middle East uses Saturday as the first day of the week, so by defining the first week as containing the first Friday then Week 1 will always include January 1st.

Europe have generally adopted the ISO standard.

To confuse it more, Week 24 of 2014 may be written as ‘2414’ or ‘1424’!

What to do

Unless you do a lot of work and plenty of research it appears to me that you could be easily caught out by all this, and the consequences may well be serious. My advice to software developers is

  • Understand there are differences in the definition of Week Numbers
  • Do not assume your language function call will be correct
  • Unless the customer asks and understands what Week Number you are displaying then think about not displaying it.
  • Ensure Week Number display can be turned off – just in case.
  • If you really have to include Week Numbers in a system that is globally available then make the rules customisable (and best of luck writing that!).

 

Les Kendall is software developer for RobotBooker.com, web-based meeting room booking systems.