Qbasicnews.com
October 18, 2018, 02:36:45 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Compute week number.  (Read 9853 times)
Moneo
Na_th_an
*****
Posts: 1971


« on: July 05, 2008, 09:39:20 PM »

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
« Last Edit: July 11, 2008, 07:14:57 PM by Moneo » Logged
Clippy
Member
*
Posts: 84



« Reply #1 on: July 22, 2008, 09:03:47 PM »

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.
« Last Edit: July 22, 2008, 09:11:07 PM by Clippy » Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #2 on: July 23, 2008, 11:39:09 PM »

Ted,

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

Regards..... Moneo
Logged
Clippy
Member
*
Posts: 84



« Reply #3 on: July 24, 2008, 05:14:32 PM »

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
Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #4 on: July 24, 2008, 10:07:25 PM »

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
Logged
Clippy
Member
*
Posts: 84



« Reply #5 on: July 25, 2008, 04:08:04 PM »

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
« Last Edit: July 25, 2008, 04:16:09 PM by Clippy » Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #6 on: July 27, 2008, 03:46:22 PM »

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
Logged
Clippy
Member
*
Posts: 84



« Reply #7 on: July 28, 2008, 03:29:58 PM »

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".
« Last Edit: July 28, 2008, 10:23:35 PM by Clippy » Logged

Moneo
Na_th_an
*****
Posts: 1971


« Reply #8 on: July 28, 2008, 09:05:00 PM »

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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!