Alpha Video Training
Results 1 to 12 of 12

Thread: Date question: calculate tuesdays in month

  1. #1
    Shahin Cassim
    Guest

    Default Date question: calculate tuesdays in month

    Hi All,

    Can someone point me in the right direction. I am getting to grips with the Xbasic Animal and dont know it quite well yet.

    How can find the number of tuesdays in any given month for any year.

    I am writing a payslip application that needs to cater for a whole wide range of needs.

    Second issue: what the best way to base calculation of this sort per record. If some people only work tuesday and others work fridays and saturday, etc.

    Regards and thanks in advance
    Shahin
    Johannesburg-South Africa

  2. #2
    "Certified" Alphaholic
    Real Name
    William Hanigsberg
    Join Date
    Apr 2000
    Location
    Toronto, ON
    Posts
    4,018

    Default RE: Date question: calculate tuesdays in month

    Shahin,

    This is certainly not beautiful but does appear to work. There are a lot of date functions in Alpha5 and one can usually get something together to solve a problem.

    Bill

  3. #3
    "Certified" Alphaholic
    Real Name
    William Hanigsberg
    Join Date
    Apr 2000
    Location
    Toronto, ON
    Posts
    4,018

    Default RE: Date question: calculate tuesdays in month

    I should probably be kept away from calendars. In addition to being confused about the current year I think there must always be 4 tuesdays in the month--even for Feb and even if Feb 1 falls on Wed.

    So one of the conditions of the function can be eliminated in which case you could just use if():

    'Date Created: 13-Jul-2003 09:46:15 AM
    'Last Updated: 13-Jul-2003 10:08:34 AM
    'Created By :
    'Updated By :

    FUNCTION Howmanytuesdays2 AS N (inputMonth AS N,inputYear as N )
    on error goto 0

    dim nTuesdays as n
    nTuesdays=if(isdate(dtoc(nth_dow("Tuesday",inputMonth,inputYear,5))),5,4)
    Howmanytuesdays2=nTuesdays

    END FUNCTION

  4. #4
    Shahin Cassim
    Guest

    Default RE: Date question: calculate tuesdays in month

    Thanks a million for the help guys.

    Appreciate it :-)

    /Shahin

  5. #5
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default RE: Date question: calculate tuesdays in month

    Hi Bill,

    As I've said, and know you've been present, the definition of the parameters of a function is always the most important step. Your example was

    FUNCTION Howmanytuesdays2 AS N (inputMonth AS N,inputYear as N )
    on error goto 0

    dim nTuesdays as n
    nTuesdays=if(isdate(dtoc(nth_dow("Tuesday",inputMonth,inputYear,5))),5,4)
    Howmanytuesdays2=nTuesdays

    END FUNCTION


    This requires passing 2 parameters, probably related. So a better choice would be

    FUNCTION Howmanytuesdays2 AS N (inputDate AS D)

    where the day is ignored. But let's take it to the next step. Suppose it was not tuesday, but wednesday. Thus it should be something like

    FUNCTION Howmanydow AS N (cdayofweek as C,inputDate AS D)

    But this is sort of stupid, as the name of the day of week is almost never passed as a parameter (but is used as a display for humans), but the number of the day-of-the-week, a value from 1 thru 7 (and 0 for blank dates). This might be a better design of a function';

    FUNCTION Howmanydow AS N (dayofweek as N,inputDate AS D)

    And what about error checking? You have not accounted for invalid months (values only 1->12) or valid years (>=0), or a blank date.

    Now let's look at the return value. In your expression it can be 4 or 5. Since it has only 2 values (except for error values) another consideration might be is to return true or false, rather than 4 or 5. But since errors are possibly important, return values probably should be -1 for an error, 0 for a blank date, and 4 or 5 for any valid date

    There's a good chance that we might want to use the today's date as a default (and maybe "monday" as a default dow), so the parameters would look like;

    FUNCTION Howmanydow AS N (dayofweek=2 as N,inputDate=DATE() AS D)

    The next step, would be to allow users a choice of a dayofweek being a number or character, so the final code would look something like this;

    FUNCTION Howmanydow AS N (dayofweek=2 as N,inputDate=DATE() AS D)

    ' Convert DayOfWeek to a number between 1 and 7
    IF typeof(DayOfWeek)=="N"
    ' Numeric type, so use as day of week
    IF between(DayOfWeek,1,7)
    ' between 1 and 7 use for Sun thru sat respectively
    dayno=DayOfWeek
    ELSE
    ' Otherwise exit with error
    END
    END IF
    ELSE IF typeof(DayOfWeek) $ "C"
    ' Character type
    dim tmp as C
    tmp=alltrim(DayOfWeek)
    IF trim(DayOfWeek)==""
    END
    END IF
    'convert string to dow number
    dayno=day_number(tmp) ' blank string returns a 1, not a zero so exit before this line
    ' If not between 1 and 7, exit with error
    IF .not.(between(dayno,1,7))
    END
    END IF
    ELSE
    ' Other types we don't know how to handle, so exit with error
    END
    END IF

    Howmanydow=if(isdate(dtoc(nth_dow(cdow(dayno),Month(inputDate),Year(inputDate),5))),5,4)

    END FUNCTION



    Just a dissertation on good coding practice. It can be taken a lot further, but this is enough for now :?)

    Regards,

    Ira

  6. #6
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default RE: Date question: calculate tuesdays in month

    Bill,

    Oops, the first line of the final function is suppose to be

    FUNCTION Howmanydow AS N (dayofweek=2 as A,inputDate=DATE() AS D)

    As the 1st parameter can be any type.

    Regards,

    Ira

  7. #7
    "Certified" Alphaholic
    Real Name
    William Hanigsberg
    Join Date
    Apr 2000
    Location
    Toronto, ON
    Posts
    4,018

    Default RE: Date question: calculate tuesdays in month

    Hi again Ira,

    Let me say that I for one appreciate your dissertation on good coding practice. I always copy out the answers you provide and I have copied this one as I admire the formal and systematic quality of your functions. I always learn from them.

    In my defense I would only say that within the strictly limited context of the question I made a conscious decision to omit elements of good programming.

    Specifically, as you point out, a separate function for Tuesdays is a bit silly since the need to count other days of the week can hardly be dismissed. As well, all functions should return something useful in case of error. You taught me this and I took it to heart.

    Where we might disagree is on the narrow issue of whether this level of generality was necessary given the context and specificity of the original posted question.

    All best wishes,
    Bill

  8. #8
    Thomas Henkel
    Guest

    Default RE: Date question: calculate tuesdays in month

    I had a need a long time ago to know how many of each day there was in any specific month in any specific year. I created an application that would build a table with day counts in it by month. I have atached it. The table is daymnth.dbf

    Tom

  9. #9
    Thomas Henkel
    Guest

    Default RE: Date question: calculate tuesdays in month

    I forgot to add that instead of going through all of that xbasic, all you need to do is a lookup to the table to get the counts for any given month.

  10. #10
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default RE: Date question: calculate tuesdays in month

    Tom,

    It's not alot of xbasic code. The operative code (without the type conversion code which just makes it a more generic function) is;

    Howmanydow=if(isdate(dtoc(nth_dow(cdow(dayno),Month(inputDate),Year(inputDate),5))),5,4)

    You've done something similar in your Calc Days although it spends a lot of time looping. You've just traded an on-the-fly calculation for a pre-calculation.

    A lookup to the table (using a lookupx() function, as opposed to a field rule lookup) is probably more complicated that a simple function call, but it requires you to use that lookup code somewhere.

    But any table reference has a more serious drawback. Sharing a database (even one that is read-only) causes more network traffic. Network traffic is always at a premium in larger systems, although in smaller systems it does not matter.

    The nth_dow() function used in the xbasic code is not a very fast function as functions go, but it's still a whole lot faster than sequentially going through every day of a month (although, much harder to derive the proper algorithm)

    Hope this guides you a bit for future application designs

    Regards,

    Ira

  11. #11
    Thomas Henkel
    Guest

    Default RE: Date question: calculate tuesdays in month

    My main point to designing it as a table was that we needed to know for reports, invoices, etc how many of EACH day there were in a given month. If someone worked Monday, Wednesday, and Saturday, How many days in any given month did they work? One lookup as a calc field gives me that number.

    Tom

  12. #12
    Shahin Cassim
    Guest

    Default RE: Date question: calculate tuesdays in month

    Thanks a stack Thomas,

    After reading the posts from Bill and Ira, suddenly I feel so much smaller in a world that has just got so much bigger :-)

    Regards and thanks for all the warm help.

    /Shahin

Similar Threads

  1. Month-Date Format in Conditional Frame
    By Dale Stansbery in forum Alpha Five Version 5
    Replies: 2
    Last Post: 10-31-2003, 06:32 AM
  2. Calculate Annual Evaluation date
    By David Agan in forum Alpha Five Version 5
    Replies: 2
    Last Post: 05-14-2003, 10:58 AM
  3. calculate due date
    By Rokrz in forum Alpha Four Versions 7 and 8
    Replies: 2
    Last Post: 08-01-2002, 10:39 AM
  4. Month-Year Date Format
    By Robert Sprague in forum Alpha Five Version 4
    Replies: 13
    Last Post: 11-27-2001, 01:50 AM
  5. question on DATE
    By ken tjia in forum Alpha Five Version 4
    Replies: 8
    Last Post: 06-10-2000, 06:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •