Your weeks should be numbered By , journalist and programmer |
|
|
In the United States week numbers seem to be a rare phenomenon, I was told. In Europe the opposite is the case: almost everyone in business or politics uses them. Hence, calendars and schedulers are considered to be far from complete in case week numbers are missing. In other words: programmers who want to create scheduler-like software for the European market will surely not be successful if they omit the week number issue in their applications. |
Is it a difficult matter? Not really, although things would be a lot easier if a year counted 364 days, making exactly 52 weeks. Like the year itself, week 1 would always start on the same day and date then. But unfortunately we have to deal with the fact that the first day of the first week is floating. It may even fall in December! Due to this 'irregularity' a year sometimes counts 53 week numbers. Rules A few examples: So far the rules. Now the programming stuff. Although calendar dates are not really the perfect data to do additions or substractions with, it is pretty easy to find or calculate the date that marks the beginning of the first week. The best way is to convert calendar dates to long-integer values first. I'm using a Julian-conversion for it, but there are different other ways that lead to Rome, as they say. If you would prefer any other method, for instance Microsoft's DateSerial or DateValue, feel free to alter that part of the coding. Lower and upper bound Before we're ready to compare, we need a third Julian Day Number (or DateValue), the one for the calendar date we want to connect with a week number and which parameters (year, month, day) were passed to the function. In respect with that specific JDN there are 3 possibilities:
Having done that, the rest of the calculation is the same for both, situation 2. and situation 3.: substract the lower bound JDN from JDN-of-date and (integer) divide the result by 7 to get the number of fully elapsed weeks so far. Finally add 1 for the 'going' week and there's your week number. Here is the pseudo code for this routine: SELECT CASE AS LONG JDNOfDate CASE < LowerBound ' it is week 52 or 53, but which one? ' therefore we need week one of previous year as our reference LowerBound = WeekOne(year - 1) CASE > UpperBound ' there is only one possibility: week nbr 1 FUNCTION = 1 EXIT FUNCTION END SELECT FUNCTION = ((JDNOfDate - LowerBound) \ 7) + 1 |
|
Where to download? |