Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compute week number.
#1
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
Reply
#2
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.
Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip
Reply
#3
Ted,

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

Regards..... Moneo
Reply
#4
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
Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip
Reply
#5
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
Reply
#6
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
Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip
Reply
#7
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
Reply
#8
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".
Get my QB demonstrator here: http://dl.dropbox.com/u/8440706/Q-Basics.zip
Reply
#9
(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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)