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

Number of months in date range ?

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

    Number of months in date range ?

    A puzzler.

    Assume any arbitrary beginning date.
    Assume any arbitrary ending date.
    Assume the ending date will be "after" the beginning date.

    What's the most efficient way to compute the number of calendar months (full or partial) that are present in the given range?

    Background: Have a need to aggregate sales and payment data by calendar month, across any arbitrary date range. Beginning and ending dates may or may not be the first or last of a month. They may or may not be in the same calendar year. I want to populate an array that contains an element for each month (or partial month) in the date range. I need to know how to size my array, so I need to know the number of full or partial months in the date range.

    Enjoy.

    -- tom

    #2
    Re: Number of months in date range ?

    Tom,
    I'm not sure I know the most efficient method to do this, but here's a thought.
    Arbitrarly choose a base year - e.g. 2000.
    get diff between year of start date and base year.Multiply by 12 and add the month of the start date.
    Do the same for the end date.
    subtract the start date months from the end date months.
    I'm sure Ira or other will come up with a very clever solution and I may be sorry for submitting this feeble solution.
    Good Luck
    John

    Comment


      #3
      Re: Number of months in date range ?

      Hi John and Tom,

      Originally posted by JohnZaleski View Post
      I'm sure Ira or other will come up with a very clever solution and I may be sorry for submitting this feeble solution.
      Nahhh, I won't come up with a solution, I already had many years ago

      The solution I posted on the message board is at AgeMonths function - difference in months

      Because of the start and end month's day of the month potentially being different, it actually is harder than one might think!
      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


        #4
        Re: Number of months in date range ?

        Tom:
        I did post a UDF in the past. I will dig it out and re-post it.
        Not sure though if you want to calculate a partial month as a month?
        If so, you could modify the UDF.

        Well now.. instead of one, I found 2. Not sure why I wrote 2 different ones, most likely one of them is wrong or maybe to serve different purposes.
        Take a look and see which works for you.
        Code:
        d1={1/16/10}
        d2={4/5/11}
        ?Month_diff(d1,d2)
        = 14
        ?Months_Between(d1,d2)
        = 15
        Code:
        FUNCTION Month_diff AS N (Start AS D, End AS D )
        	Month_diff=12*(year(end)-year(start))+month(end)-month(start)+if(day(end)<day(start),-1,0)
        END FUNCTION
        Code:
        FUNCTION Months_Between AS N (Start AS D, End AS D )
        	x=0
        	while start<end
        		start=addmonths(month_start(start),1)+day(start)-1
        		x=x+1
        		end while
        		months_between=x
        END FUNCTION
        Last edited by G Gabriel; 01-02-2010, 08:36 PM.

        Comment


          #5
          Re: Number of months in date range ?

          Thank, gents.

          I'll be using G's Months_Between(). Was the quickest for me to grasp.

          Thanks to all.

          -- tom

          Comment


            #6
            Re: Number of months in date range ?

            Tom:
            This function is no good.
            I replaced it with this one which seems to work OK.
            Code:
            FUNCTION Months_Between AS N (Start AS D, End AS D )
            	x=if(start=month_end(start).and.end=month_end(end),1,0)
            	while start<end
            		end=addmonths(end,-1)
            		x=if(end>start,x+1,x)
            		end while
            		months_between=x
            END FUNCTION
            Code:
            d1={1/31/10}
            d2={2/27/10}
            ?Months_Between(d1,d2)
            = 0
            d2={2/28/10}
            ?Months_Between(d1,d2)
            = 1
            d2={3/28/10}
            ?Months_Between(d1,d2)
            = 1
            d2={3/31/10}
            ?Months_Between(d1,d2)
            = 2
            d2={6/29/10}
            ?Months_Between(d1,d2)
            = 4
            d2={6/30/10}
            ?Months_Between(d1,d2)
            = 5
            d2={11/30/10}
            ?Months_Between(d1,d2)
            = 10
            d2={3/28/11}
            ?Months_Between(d1,d2)
            = 13
            Last edited by G Gabriel; 01-03-2010, 03:32 AM.

            Comment


              #7
              Re: Number of months in date range ?

              what is an example of where this would fail:

              Code:
              FUNCTION Months_Between AS N (Start AS D, End AS D )
               while start<end
                start=addmonths(start,1)
                x=x + 1
               end while
               months_between=x
              END FUNCTION
              I tried all the functions, including this one, as well as Ira's, and went all the way back to date ranges from 1943 thru today - all returned exactly the same number of months.

              Never mind - now I see the reason for Gabe's last post.
              Last edited by martinwcole; 01-03-2010, 04:38 AM.
              Cole Custom Programming - Terrell, Texas
              972 524 8714
              [email protected]

              ____________________
              "A young man who is not liberal has no heart, but an old man who is not conservative has no mind." GB Shaw

              Comment


                #8
                Re: Number of months in date range ?

                No exactly.. needed one more tweak:
                Code:
                FUNCTION Months_Between AS N (Start AS D, End AS D )
                	dim x as n
                	x=if(end=month_end(end).and. day(end)<=day(start),1,0)
                		while start<end
                		end=addmonths(end,-1)
                		x=if(end>start,x+1,x)
                		end while
                		months_between=x
                END FUNCTION
                Code:
                d1={1/28/10}
                d2={2/28/10}
                ?Months_Between(d1,d2)
                = 1
                d2={2/27/10}
                ?Months_Between(d1,d2)
                = 0
                Hopefully, there is no more tweaking to do it's time for me to sign out for now.
                Martin:
                Try it with the examples I gave, it will fail in some.
                P.S.
                Either the message board time is off by one hour, or it's really time for me to sign off! I think both.

                Comment


                  #9
                  Re: Number of months in date range ?

                  It's a puzzler for sure.

                  I decided to use a brute force approach that ignores the day component in each input date. Seemed simpler to my way of thinking. Here's what I'll be using.

                  Thanks again for your help. Hope you enjoyed the exercise!

                  -- tom

                  Code follows:
                  Code:
                  'Date Created: 02-Jan-2010 08:06:09 PM
                  'Last Updated: 03-Jan-2010 07:24:38 AM
                  'Created By  : Tom Cone Jr
                  'Updated By  : Tom Cone Jr
                  FUNCTION Months_within AS N (start AS D, end AS D )
                  	
                  'PURPOSE: Given any arbitrary start and end dates, return the number of calendar
                  '         months (full or partial both count) within the date range.
                  '
                  'ASSUMES: end date will fall after start date
                  '
                  'APPROACH:	Brute force.  Start with the start date and increment months until you get beyond
                  '                         the end date; count the number of times incrementing occurs
                  '=====================================================================================
                  	x = 0									'x is the count that will be returned by the Fn
                  	
                  	s_yyyymm = left(dtos(start),6)			' "201001"  - extract YYYYMM from start date
                  	e_yyyymm = left(dtos(end),6)			' "201006"	- extract YYYYMM from end date
                  	
                  	vn_s_yyyymm = val(s_yyyymm)				' 201001 	- convert to numeric
                  	vn_s_yyyy = val(left(s_yyyymm,4))		' 2010		- convert to numeric	
                  	vn_s_mm = val(right(s_yyyymm,2))		' 1			- convert to numeric NOTE absence of leading zero
                  	vn_e_yyyymm = val(e_yyyymm)				' 201006	- convert to numeric 
                  	
                  	while vn_s_yyyymm <= vn_e_yyyymm		'loop until the numeric equivalent of YYYYMM is greater 
                  										'	than the YYYYMM equivalent of the end date
                  										
                  		x = x + 1						'increment counter
                  		vn_s_mm = vn_s_mm + 1			'increment month - will NOT have leading zero
                  		if vn_s_mm > 12 then			'if more than 12 set to 1 (january) and
                  										'   also increment the year
                  		    vn_s_mm = 1					'again no leading zero
                  		    vn_s_yyyy = vn_s_yyyy + 1
                  		end if
                  
                  		'now rebuild the six digit string so it can be converted to numeric and tested
                  		vc_s_yyyymm = ltrim(str(vn_s_yyyy)) + padl(ltrim(str(vn_s_mm)),2,"0")
                  		vn_s_yyyymm = val(vc_s_yyyymm)	'convert for comparison
                  		
                  		'trace.writeln(ltrim(str(vn_s_yyyymm)) + ", " + ltrim(str(vn_e_yyyymm)))
                  	end while	 
                  		
                  	months_within = x
                  END FUNCTION

                  Comment


                    #10
                    Re: Number of months in date range ?

                    Tom,
                    I would not characterize it as puzzling.If you use Ira's as he called it "trivial" solution,It works perfectly wth the following script.


                    'rem test months between
                    loopdate = date()
                    countbad = 0

                    for x = 1 to 2000
                    loopdate = loopdate + x
                    end_date = addmonths(loopdate,x)
                    'rem Ira's trivial solution
                    calc_mos =(YEAR(end_date)*12)+MONTH(end_date)-(YEAR(loopdate)*12)-MONTH(loopdate)

                    if calc_mos <> x then
                    countbad = countbad + 1
                    end if
                    next x
                    ui_msg_box("bad date count",alltrim(str(countbad)))
                    end

                    John

                    Comment


                      #11
                      Re: Number of months in date range ?

                      Hi, John.

                      Perhaps. I'm less intuitive than I used to be. The absence of comments, and the use of terms like "bad date count" and "countbad" were enough to be perplexing. Could not easily see how to integrate the script into a function. This probably reveals more about me than I would otherwise wish, but I'm tired and have a cold, so it is what it is.

                      It's good to hear from you again.

                      Best wishes for a happy, healthy and prosperous new year.

                      -- tom

                      Comment


                        #12
                        Re: Number of months in date range ?

                        One more. I believe this works.

                        Code:
                        FUNCTION months_in_range AS N (d_start as D, d_end as D )
                        
                        dim x as n
                        if year(d_end)=year(d_start)					
                            ' within the same year
                            x=month(d_end)-month(d_start)+1
                        else
                            ' lapsing years
                            x=((13-month(d_start))+month(d_end))+((year(d_end)-year(d_start)-1)*12)
                        end if
                        	
                        months_in_range=x
                        
                        END FUNCTION
                        Code:
                        dim vds as D={11/09/2006}
                        dim vde as D={11/17/2008}
                        
                        ? months_in_range(vds,vde)
                        = 25
                        Mike W
                        __________________________
                        "I rebel in at least small things to express to the world that I have not completely surrendered"

                        Comment


                          #13
                          Re: Number of months in date range ?

                          Hi Mike,

                          Originally posted by Mike Wilson View Post
                          One more. I believe this works.
                          I'm confused ! Is there a point to the continuation of the thread?
                          • Is it just an exercise to try?
                          • Does my posted archive code not work?
                          • Or something else?
                          One doesn't need to fully comprehend a function to use it:) !
                          What am I missing?

                          Thanks!
                          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


                            #14
                            Re: Number of months in date range ?

                            Tom,
                            The script I submitted serves no useful purpose other than to satisfy one on the consistent accuracy of Ira's expression to satisfy your stated requirements.All Ira's expression does is calculate the total months from date1 from the total months from date2.My poorly commented script was meant to test this concept over a wide range of dates.It added nothing to Ira's expression and there would be no earthly reason to make a function out of it.
                            Happy New Year to you also.
                            John

                            Comment


                              #15
                              Re: Number of months in date range ?

                              Originally posted by csda1 View Post
                              I'm confused ! Is there a point to the continuation of the thread?
                              *Is it just an exercise to try?
                              *Does my posted archive code not work?
                              *Or something else?

                              One doesn't need to fully comprehend a function to use it:) !
                              What am I missing?

                              Thanks!
                              Correct... your posted archive does not generate the value that Tom was looking for.
                              What's the most efficient way to compute the number of calendar months (full or partial) that are present in the given range?
                              for date1 as D={11/09/2006} and date2 as D={11/17/2008} the answer is 25. Neither of yours produce that.
                              Also trying to fit into Tom's words of...
                              Was the quickest for me to grasp.
                              Mike W
                              __________________________
                              "I rebel in at least small things to express to the world that I have not completely surrendered"

                              Comment

                              Working...
                              X