Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Date question: calculate tuesdays in month

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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
    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
    Bill Hanigsberg

    Comment


      #3
      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
      Bill Hanigsberg

      Comment


        #4
        RE: Date question: calculate tuesdays in month

        Thanks a million for the help guys.

        Appreciate it :-)

        /Shahin

        Comment


          #5
          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
          Regards,

          Ira J. Perlow
          Computer Systems Design


          CSDA A5 Products
          New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
          CSDA Barcode Functions

          CSDA Code Utility
          CSDA Screen Capture


          Comment


            #6
            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
            Regards,

            Ira J. Perlow
            Computer Systems Design


            CSDA A5 Products
            New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
            CSDA Barcode Functions

            CSDA Code Utility
            CSDA Screen Capture


            Comment


              #7
              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
              Bill Hanigsberg

              Comment


                #8
                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

                Comment


                  #9
                  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.

                  Comment


                    #10
                    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
                    Regards,

                    Ira J. Perlow
                    Computer Systems Design


                    CSDA A5 Products
                    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
                    CSDA Barcode Functions

                    CSDA Code Utility
                    CSDA Screen Capture


                    Comment


                      #11
                      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

                      Comment


                        #12
                        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

                        Comment

                        Working...
                        X