Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Weeks in a year
Message
From
17/10/2006 19:27:40
 
 
To
17/10/2006 16:51:43
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01162585
Message ID:
01162767
Views:
26
This message has been marked as the solution to the initial question of the thread.
Hi all,

additional to the 31st december of the year possibly being in week 1 of the following year, the 1st january of a year can also belong to the last week of the previous year, because a week number is assigned to each full week, so the week in which the year changes is not week 52 or 53 at the end of one year and week 1 at the beginning of the next year, it's in most cases counted for the year in which most days of that week lie.

The root of it is the first day of the first week of a year. You may define that in different ways, that's what the second and third parameter of the Week() function are there for.

In most countries I know monday is considered 1st day of a week and the first week is - like already said - the one, which has the larger part (at least 4 days) in the year of question.

So the algorithm to calculate the weeks should be: find out the day of week of 31st of december. If that is only day 1,2 or 3 of a week, that week has at least 4 days in the next year, so you'd need to subtract that amount of days from 31st december to get the last day of the last week of this year. Otherwise you can take the week number from the 31st of december.

If we define a constant cnFirstDayOfWeek, then the function would be:
? Weeks(2006)
? Weeks(2007)
? Weeks(2008)
? Weeks(2009)
Function Weeks(tnYear)
   #Define cnFirstDayOfWeek 2  && weeks start on monday
   Local ldLastday, lnDoW
   ldLastday = Date(tnYear,12,31)
   lnDoW = DoW(ldLastday,cnFirstDayOfWeek)
   Return Week(ldLastday-iif(lnDoW<4,lnDow,0),2,cnFirstDayOfWeek)
Endfunc
Please note I intentionally didn't define a constant cnFirstWeek for the second parameter of the Week() function. If one would choose the first week to have 1st january in it or to have 7 days in it (option 1 or 3 instead of 2), the algorithm would be totally wrong, so it wouldn't help making this adjustable.

With the option, that the first week must have 7 days, the 31st of december is guaranteed to be within the last week of a year, so then it would simply be Week(tnYear,12,31).

With the option, 1st january is part of week one, you'd need to subtract it's dow() from itself to have the last day of the last week of the previous year, so Weeks() would be Week(date(tnYear+1,1,1)-dow(date(tnYear+1,1,1),cnFirstDayOfWeek),1,cnFirstDayOfWeek).

Now all you need to know is, how it's defined for your country. The function I layed out first is the ISO week count of a year, if you stay with monday as first day of a week.

Bye, Olaf.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform