Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Computing last day of the month in PB

Status
Not open for further replies.

marineman

Programmer
Jul 2, 2010
11
US
Here's my small contribution to this very helpful PowerBuilder site:

a simple function to find the last day of month of any given date:


f_get_mo_last_day ( date arg_date ) returns date
Code:
date incr_date
integer curr_mo

curr_mo = month(arg_date)
incr_date = arg_date

do while curr_mo = month(incr_date)
  incr_date = relativedate(incr_date,1)
loop

return relativedate(incr_date, -1)

I couldn't find this function in the PowerScript functions so I wrote my own. If there is another ways to do this, I'd be happy to hear from anybody.

 
I've seen a number of ways to do this, especially in SQL.
Here is what I have.


Code:
int li_month, li_year, li_last_day
string ls_test_date

li_month = Month(arg_date)
li_year = Year(arg_date)
CHOOSE CASE li_month
   CASE 1,3,5,7,8,10,12
      li_last_day = 31
   CASE 4,6,9,11
      li_last_day = 30
   CASE 2
      li_last_day = 30
      DO
        li_last_day = li_last_day - 1
        ls_test_date = string(li_month) + '/' + string(li_last_day) + '/' + string(li_year)
      LOOP UNTIL IsDate(ls_test_date)
END CHOOSE
ls_test_date = string(li_month) + '/' + string(li_last_day) + '/' + string(li_year)
RETURN Date(ls_test_date)

Matt

"Nature forges everything on the anvil of time"
 
Thank you Matt!

Very nice!

I see far fewer loops in your algorithm than mine

 
I did also something like MarineMan, but without using a loop, simply incrementing the month of the date from which you want to substract -1 at the end of the code. Just add one to the month, (if = 13 then instead sum 1 to year and month=1) and creating the new date as day 1, month (the one obtained after adding 1 to month) and year (the one obtained after adding 1 to month.

That'll give you the date you can return like:
return relativedate(incr_date, -1)

It might be slighty faster if you have to call this function lots of times, for not using the loop.

If you're lazy like me, then tell me and I'll write down the function.

;)
Miguel


regards,
Miguel L.
 
If you want to test if a date is the last day of the month:

/*******************************************
datetime ldt_datetime
/* ldt_datetime could be a param */

IF Day(Date(ldt_datetime)) < Day(RelativeDate(Date(ldt_datetime),1)) THEN
MessageBox("Attention!","The date you entered is not the last day of the month.")
END IF
/*******************************************

KISS: Keep It Simple and Stupid
+ its useable in an Expression of a Datawindow.

/*******************************************
IF(Day(Date(##FIELD##)) < Day(RelativeDate(Date(##FIELD##),1),1,0)

Sorry if my english isnt that good... i made my best to translate.

Happy new year!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top