# Thread: Last Day Of The Month Code

1. ## 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. ## RE: Last Day Of The Month Code

i would do something like this

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

end function

Hope this helps!

Regards,

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

5. ## 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. ## 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. ## RE: Last Day Of The Month Code

Michael:

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

Gary

8. ## RE: Last Day Of The Month Code

Ira:

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

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

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

Cheers

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

#### Posting Permissions

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