I was in need of calculating the calendar week for one of my applications. Sounds easy but finally I spent lots of hours the last days ... :sign0148:
There are lots of algorithms and examples on the net, but most of them don't work perfectly . The treatment of fractured weeks in the beginning and end of a year can be very complicated. But now I have an algorithm that works (as far as I can tell...)
It covers ISO 8601 standard (and the german DIN 1355 standard), so it should be suitable for most European countries. Calculation rules:
Therefore this algorithm will not work for countries where week starts with Sunday (USA?) or the first week bases on January 1 (UK?). More info and links here: http://en.wikipedia.org/wiki/Week
Cheers,
Stefan
There are lots of algorithms and examples on the net, but most of them don't work perfectly . The treatment of fractured weeks in the beginning and end of a year can be very complicated. But now I have an algorithm that works (as far as I can tell...)
It covers ISO 8601 standard (and the german DIN 1355 standard), so it should be suitable for most European countries. Calculation rules:
- Week starts with Monday.
- The first week must have 4 days in January. In other words: the first week must contain the first Thursday of the year. And January 4.
- Only years starting or ending with Thursday can have a week #53.
Therefore this algorithm will not work for countries where week starts with Sunday (USA?) or the first week bases on January 1 (UK?). More info and links here: http://en.wikipedia.org/wiki/Week
Cheers,
Stefan