Alpha Video Training
Results 1 to 14 of 14

Thread: Last Day Of The Month Code

  1. #1
    robert bannerman
    Guest

    Default Last Day Of The Month Code

    I have written the following expression for anyone who neeeds to find the last day of a month:

    IF(LEN(ALLTRIM(RIGHT(DTOC(CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/31/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2)))),2)))>0,CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/31/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2))),IF(LEN(ALLTRIM(RIGHT(DTOC(CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/30/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2)))),2)))>0,CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/30/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2))),IF(LEN(ALLTRIM(RIGHT(DTOC(CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/29/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2)))),2)))>0,CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/29/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2))),CTOD(ALLTRIM(STR(MONTH(EDATE),2,0))+"/28/"+ALLTRIM(RIGHT(STR(YEAR(EDATE),4,0),2))))))

    Substitute EDATE (a date filed value) with your date, and you get the end of the month date.

    If anyone knows of a more efficient way or function in A5 let me know.

  2. #2
    Member
    Real Name
    Michael Pesach
    Join Date
    Apr 2000
    Posts
    145

    Default RE: Last Day Of The Month Code

    i would do something like this

    addmonths(ctod(ut(str(month(edate)))+chr(92)+"01"+chr(92)+ut(str(year(edate)))),1)-1

  3. #3
    Volunteer Moderator drgarytraub's Avatar
    Real Name
    Dr. Gary Traub
    Join Date
    May 2000
    Location
    Boca Raton, FL
    Posts
    2,772

    Default RE: Last Day Of The Month Code

    I don't know if this is "more efficient", but here is the method that I use:

    m=month(date())

    if m=1
    mstr="31"
    end if

    if m=2
    if year(date())=2000 .or. year(date())=2004 .or. year(date())=2008 .or. year(date())=2012
    mstr="29"
    else
    mstr="28"
    end if
    end if


    if m=3
    mstr="31"
    end if

    if m=4
    mstr="30"
    end if

    if m=5
    mstr="31"
    end if

    if m=6
    mstr="30"
    end if

    if m=7
    mstr="31"
    end if

    if m=8
    mstr="31"
    end if

    if m=9
    mstr="30"
    end if

    if m=10
    mstr="31"
    end if

    if m=11
    mstr="30"
    end if

    if m=12
    mstr="31"
    end if

    mstr="-"+mstr+"-"

    date2=ctod(alltrim(str(month(date()))+mstr+alltrim(str(year(date())))))

    Gary Traub

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

    Default RE: Last Day Of The Month Code

    Robert, Michael & Gary,

    There are a couple of problems with all your approaches
    1. CTOD() converts differently depending upon American or European date formats
    2. You have not accounted for the blank date case


    function MonthEnd as D(DateVal as D)

    ' Returns date of Last day of DateVal's month or blank

    MonthEnd=iif(DateVal={},{},Addmonths(DateVal-Day(DateVal)+1,1)-1)

    end function


    Hope this helps!

    Regards,

    Ira J. Perlow
    Computer Systems Design & Associates
    csda@mediaone.net

  5. #5
    robert bannerman
    Guest

    Default RE: Last Day Of The Month Code

    I don't ever use European settings, but I appreciate your suggestion, and will try it tomorrow.

    How do I implement the function in the table, or browse?

    I've tried the action script in the field rules, but it hasn't worked for me (I may be doing something wrong).

  6. #6
    Member
    Real Name
    Daryl Grace
    Join Date
    Apr 2000
    Location
    Hopkinsville, KY
    Posts
    165

    Default RE: Last Day Of The Month Code

    Robert,

    I believe you have to create a global function. Then in field rules, forms, reports, etc. MonthEnd({06/01/2000})

    Daryl

  7. #7
    Volunteer Moderator drgarytraub's Avatar
    Real Name
    Dr. Gary Traub
    Join Date
    May 2000
    Location
    Boca Raton, FL
    Posts
    2,772

    Default RE: Last Day Of The Month Code

    Michael:

    Your script is ingenius! Works beautifully. Thanks ...

    Gary

  8. #8
    Volunteer Moderator drgarytraub's Avatar
    Real Name
    Dr. Gary Traub
    Join Date
    May 2000
    Location
    Boca Raton, FL
    Posts
    2,772

    Default RE: Last Day Of The Month Code

    Ira:

    Actually, your function is the one I was talking about. Very consice, and efficient. Thanks!

  9. #9
    Former Alpha Employee JerryBrightbill's Avatar
    Real Name
    Jerry Brightbill
    Join Date
    Apr 2000
    Posts
    5,171

    Default RE: Last Day Of The Month Code

    Ira,
    Thanks for a great expression! I use a similar approach, but yours accounts for any date format. The simplest approach is always the best.

    Jerry

  10. #10
    robert bannerman
    Guest

    Default RE: Last Day Of The Month Code

    Thanks a lot.

    The function works fine (I used a prior thread to figure out how to incorporate the function), but the halt script message appears each time I re-evaluate the field rules.

    Is that normal?

    If so, how do I prevent it?

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

    Default RE: Last Day Of The Month Code

    Robert,

    Anytime a script runs, A5HALT will pop up if the A5HALT.DLL file is present. This allows you to abort script execution if the script hangs. I presented the solution as global script function, but in the field rules you'd be better served just to use the expression with the appropriate Date field substituted for the DateVal field, as in

    iif(DateField={},{},Addmonths(DateDield-Day(DateField)+1,1)-1)

    The point is that if the expression is all in one (as opposed to multiple lines of script code, you can just use the expression.

    Just because I presented the expression in a global function, doesn't mean you can't use just the expression. You may be unfamiliar with A5, but try to extrapolate from what's presented. You will seldom ever see the code exactly as you need it.

    Regards,

    Ira J. Perlow
    Computer Systems Design & Associates
    csda@mediaone.net

  12. #12
    Derek
    Guest

    Default RE: Last Day Of The Month Code

    Ira:

    I don't write too many scripts, but I am curious about the one you wrote here. I copied it into my A5V1.2 and compiled it.... that went ok.

    But I wanted to see the value so I tried to use ui_msg_box(,,) but then it wouldn't compile.

    First I tried to display it with: ui_msg_box("date is",dtoc(MonthEnd), when that didn't work, I tried the following (which also wouldn't work)

    test=CTOD(MonthEnd)
    ui_msg_box("date is",test)

    They both compiled, but both returned the following error message "invalid variable type"

    I attached the little script to a button (push).

    Any ideas how I can get this to display?

    Thanks in advance.

    Cheers

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

    Default RE: Last Day Of The Month Code

    Derek,

    The way I made the MonthEnd function is that you must give it a date value (of date type) and it returns the end of the month for the date value as another date value. Thus to get the end of the current month, you would use the system date, which is returned by the built-in function DATE(). If you want to convert it to a character type (for display in ui_msg_box etc), you must use DTOC() function. Thus the expression would look like

    ui_msg_box("date is",dtoc(MonthEnd(Date())))

    Of course you could use a date type field or any valid date expression substituted for DATE().

    Regards,

    Ira J. Perlow
    Computer Systems Design & Associates
    csda@mediaone.net

  14. #14
    Volunteer Moderator drgarytraub's Avatar
    Real Name
    Dr. Gary Traub
    Join Date
    May 2000
    Location
    Boca Raton, FL
    Posts
    2,772

    Default RE: Last Day Of The Month Code

    Derek:

    To your little script, you need to add the following:

    dim test as d

    so that the variable "test" is recognized as a date variable.

    Also, in your first script, you are missing one parenthesis at the very end, i.e., try:

    ui_msg_box("date is",dtoc(MonthEnd))

    Hope this helps ...

    Gary

Similar Threads

  1. Help in month balance
    By Arturo Cort├ęs in forum Alpha Five Version 6
    Replies: 1
    Last Post: 02-11-2005, 06:49 PM
  2. Last day of the month
    By George Corder in forum Alpha Five Version 5
    Replies: 7
    Last Post: 05-28-2004, 03:33 PM
  3. Sort by month and day
    By Richard Siferd in forum Alpha Five Version 5
    Replies: 3
    Last Post: 11-13-2003, 02:40 AM
  4. Dates by day in a month
    By cpc@technonet.com in forum Alpha Five Version 5
    Replies: 1
    Last Post: 04-02-2003, 05:04 AM
  5. Week of the month
    By nick marodis in forum Alpha Five Version 4
    Replies: 12
    Last Post: 09-13-2000, 11:45 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
  •