Qbasicnews.com

Full Version: Compute week number.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I searched the forum, and there are several posts that calculate the day of the week, but I can't find any that compute the week number for a given date.

Does anybody have an algorithm for this?

By the way, I'm talking about the week number in the Gregorian calendar, not that of ISO-8601. This also implies that the starting day of the week is Sunday, as used in the USA.

Regards..... Moneo
First you gotta figure the first day of the year's weekday. Find out if it is a Leap year and then you have to figure the day of the current year you are in. You could use the ISO standard also.

Just divide the number of days by 7 and round up.
Ted,

It's not quite as simple as you make it sound. Have you tried coding and testing your approach?

Regards..... Moneo
I could when I get time. The number of days is not hard to figure if you know if it is a leap year to add one to the days in February. You just count up to the month's days for each month previous to the month you are in and add the day number of the month you are in.

Then integer divide by 7.

For instance the 6th day of the year would = 6 \ 7 . So you are in the first week. If the date is the 8th of the year, that = 8 \ 7. The second week. Just add 1 to that and you have the week number.

Here is a SUB I made to return leap years and the first day of years weekday based on 1 being Sunday and 7 being Saturday. This could be used if you want to do it the ISO way. There are plenty of Leapyear routines that are simpler! Data is based on year 2000. Can go back or forward any years too:

Code:
```SUB LeapYear (yrn%, Leap\$, firstday%) 'yrn% = year number from DATE\$ or manual entry         Â    Leap\$ = "" LpYear% = 2000 firstday% = 7Â  Â  'Â  the first day of 2000 was a saturday, the 7th day IF yrn% >= 2000 THENÂ  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  ' ** yrn% INPUT         Â  Â  'FORWARD LOOPÂ  Â  Â  Â   2000 up yrnm% = 1999Â  Â   'start in 1999Â  Â  Â  Â  Â  Â  DO COLOR 14 Â   yrnm% = yrnm% + 1Â   ' start in 2000 (1999 + 1) Â  Â  IF yrnm% > 2000 THEN firstday% = firstday% + 1 'add one day every year to first day (52 weeks + 1 Â  Â  IF firstday% > 7 THEN firstday% = firstday% - 7Â  Â   ' ** firstday% OUTPUT Â  Â  IF yrnm% = LpYear% THEN Leap\$ = "Y"Â  Â  Â  Â  Â  Â  Â  Â  ' ** Lp\$ for print only Â  Â  IF yrnm% MOD 100 = 0 THEN nly% = LpYear% MOD 400Â  'test century leap 400 Â  Â  IF nly% <> 0 THEN Leap\$ = "N"Â  Â  Â  ' if divisible by 400                         Â  Â  Â  Â  Â  IF yrnm% - 1 = LpYear% THENÂ  Â  Â  Â  Â  Â  Â  Â  Â   ' add day AFTER leap year Â  Â  Â  IF nly% = 0 THEN firstday% = firstday% + 1Â  Â  Â  Â  ' add a leap day Â  Â  Â  IF firstday% > 7 THEN firstday% = firstday% - 7 Â  Â  Â  Leap\$ = "N" Â  Â  Â  LpYear% = LpYear% + 4Â  Â  Â  Â  Â  Â  ' find next leap year by adding 4 Â  Â  END IF Â  Â  nly% = 0Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  ' reset MOD value LOOP UNTIL yrnm% = yrn% END IFÂ  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â   'end forward loop IF yrn% < 2000 THENÂ  Â  Â  Â   'REVERSE LOOPÂ  Â   2000 down         Â  yrnm% = 2001Â  Â  Â  Â  Â  Â  Â  Â   'start in 2001 DO Â   yrnm% = yrnm% - 1Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â   ' start in 2000 (2001 - 1) Â  Â  IF yrnm% < 2000 THEN firstday% = firstday% - 1Â   'subt one day every year to first day (52 weeks + 1 Â  Â  IF firstday% = 0 THEN firstday% = 7 Â  Â  IF yrnm% MOD 100 = 0 THEN nly% = LpYear% MOD 400Â  ' test century every 100 Â  Â  Leap\$ = "N" Â  Â  IF yrnm% = LpYear% THENÂ  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  ' BEFORE leap year Â  Â  Â   IF nly% = 0 AND LpYear% <> 2000 THEN firstday% = firstday% - 1Â  Â  Â  Â  ' subt a leap day Â  Â  Â   IF firstday% = 0 THEN firstday% = 7 Â  Â  Â   LpYear% = LpYear% - 4Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â   ' find next leap year Â  Â  Â   Leap\$ = "Y" Â  Â  Â   IF nly% <> 0 THEN Leap\$ = "N"Â  Â  Â  ' if divisible by 400 Â  Â  END IF Â  Â  nly% = 0Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â   ' reset MOD value LOOP UNTIL yrnm% = yrn% END IF END SUB```

Ted
Ted, you wrote:
"For instance the 6th day of the year would = 6 \ 7 . So you are in the first week."

Actually, the 6th day of the year is in the second week for 2008.

You're on the right track, but your integer division of the day of the year is missing a key factor, which is: the day of the week (1 to 7) of January 1st of the subject year.

Before integer dividing by 7, you must add the day of the week of Jan 1st, minus 2, to the day of the year of the subject date. After the integer division, you must add one to the result to get the correct week number.

As you can see, this issue gets more and more complicated.

Regards..... Moneo
Well, I said that you might have to use something like my SUB to figure the day of the week the First is. Do I have to do the entire project?

Using the first day of year week day can tell you the end of the last week also using integer division. You can MOD 7 to calculate how many days after the last week ending the actual date is.

Let me guess, you already figured it out, didn't you? LOL

I guess that is why it is a challenge..................but I recall you posting about this when you did not have it figured out.

Too busy now. I know you can do this Moneo.

Ted
You[re right, Ted. I already had an algorithm to compute the week number, which I wrote back in 1994.

I posted this challenge hoping to see a different, simpler algorithm than mine.

When you find the time to write this, I'd really be interested to see how you did it.

Regards..... Moneo
There are three ways to calculate a week number:

1) The first week has the first day in it and ends saturday. Then the next
Â  Â  Â weeks are just Sundays to Saturdays. (OOPS! edited)

2) The first week starts on the 1st and ends on the 7th. This makes the 8th
Â  Â  Â part of the second week.

3) ISO skips the first week if the first day is on a Thursday or later.

So, besides ISO, which way do you want it calculated? Sorta like "rounding".
(07-28-2008, 11:29 PM)Clippy link Wrote:There are three ways to calculate a week number:

1) The first week has the first day in it and ends saturday. Then the next
Â  Â  Â weeks are just Mondays to Saturdays.

2) The first week starts on the 1st and ends on the 7th. This makes the 8th
Â  Â  Â part of the second week.

3) ISO skips the first week if the first day is on a Thursday or later.

So, besides ISO, which way do you want it calculated? Sorta like "rounding".
The method that has always made sense to me is your method #1, with a minor correction to readÂ  "Then the next weeks are just SUNDAYS to Saturdays."

To summarize this method:
* The first week has the first day-of-the-year in it.
* The first week ends on a Saturday,
* Subsequent weeks begin on a Sunday and end on a Sarturday, except for the last week of the year, which may be shorter.

For a year beginning on a Saturday, this method will give us:
* The first week begins and ends on January 1st.
* The second week begins on January 2nd and ends on January 8th.

For a leap year beginning on a Saturday (like 1972, 2000, 2028), this method will give us:
* December 31st, the last day of the year, falls on a Sunday, and is the beginning and end of week number 54.

Regards..... Moneo