How can I get the last day of the month in progress 4gl?
How to get end of month date in progress 4gl
Asked Answered
/* the last day of this month is one day less than the first day of next month
*
* so add one month to the first day of this month and then subtract one day.
*
*/
function lastDay returns date ( input d as date ):
return add-interval( date( month( d ), 1, year( d )), 1, "month" ) - 1.
end.
add-interval() requires version 10 or better. –
Damron
DEF VAR d-Dataa AS DATE NO-UNDO FORMAT "99/99/9999".
DEF VAR c-Datai AS CHAR NO-UNDO.
DEF VAR d-Datai AS DATE NO-UNDO FORMAT "99/99/9999".
DEF VAR c-Dataf AS CHAR NO-UNDO.
DEF VAR d-Dataf AS DATE NO-UNDO FORMAT "99/99/9999".
d-Dataa=TODAY.
c-Datai=("01/" + STRING(MONTH(d-Dataa),"99/") + STRING(YEAR(d-Dataa),"9999")).
d-Datai=DATE(c-Datai).
IF MONTH(d-Dataa) = 12 THEN
c-Dataf="01/01/" + STRING(YEAR(d-Dataa) + 1,"9999").
ELSE
c-Dataf=STRING(DAY(d-Datai),"99/") + STRING(MONTH(d-Dataa)+ 1,"99/") + STRING(YEAR(d-Dataa),"9999").
d-Dataf=DATE(c-Dataf) - 1.
MESSAGE
"Dia Atual: " STRING(d-Dataa, "99/99/9999") SKIP
"Primeiro dia do Mês: " c-Datai SKIP
"Ultimo dia do Mês: " STRING(d-Dataf, "99/99/9999") SKIP
VIEW-AS ALERT-BOX
If you don't have "interval" on your platform - this'll do just as well:
DEFINE VARIABLE start-date AS DATE NO-UNDO.
DEFINE VARIABLE end-of-month AS DATE NO-UNDO.
ASSIGN
start-date = DATE(2, 15, 2012)
.
ASSIGN
end-of-month = DATE(MONTH(start-date), 20, YEAR(start-date)) + 15
end-of-month = end-of-month - DAY(end-of-month)
.
This fails if your start-date is 3/15/2012. –
Damron
DEF VAR dt-ref AS DATE NO-UNDO.
DEF VAR dt-end-of-month AS DATE NO-UNDO.
ASSIGN dt-ref = DATE(2,12,2012)
.
ASSIGN dt-ref = dt-ref + 33
dt-end-of-month = DATE(MONTH(dt-ref),1,YEAR(dt-ref)) - 1.
The easiest way is to use ADD-INTERVAL function.
ldEndDate = ADD-INTERVAL(ldBeginDate, 1, "months") - 1.
PROCEDURE getEndOfMonth:
DEFINE INPUT PARAMETER lv-date AS DATE.
DEFINE OUTPUT PARAMETER lv-monthEnd AS DATE.
lv-monthEnd = DATE(MONTH(lv-date), 31, YEAR(lv-date)) NO-ERROR.
IF lv-monthEnd = ? THEN DO:
lv-monthEnd = DATE(MONTH(lv-date), 30, YEAR(lv-date)) NO-ERROR.
IF lv-monthEnd = ? THEN DO:
lv-monthEnd = DATE(MONTH(lv-date), 29, YEAR(lv-date)) NO-ERROR.
IF lv-monthEnd = ? THEN
lv-monthEnd = DATE(MONTH(lv-date), 28, YEAR(lv-date)) NO-ERROR.
END.
END.
END.
An easy one, go the last possible day of the current month, if it fails the last day is the previous one:
def var i as int.
def var v_eom as date.
Do i = 27 to 31:
v_eom = date(month(today), i, year(today)) no-error.
If error-status:error then do:
v_eom = date(month(today), (i - 1), year(today)) no-error.
leave.
End.
End.
DEFINE VAR date1 AS DATE INIT 12/12/2015 .
DEFINE VAR datelast AS INT .
DEFINE VAR mont AS int.
mont = ( MONTH (date1) + 1).
IF mont > 12 THEN
DO:
datelast = 31.
END.
ELSE
DO:
datelast = (DATE(STRING (1) + "/" + (STRING(mont)) + "/" + STRING( YEAR(date1))) - DATE(STRING((1)) + "/" + STRING(month(date1)) + "/" + STRING( YEAR(date1)))).
END.
MESSAGE datelast VIEW-AS ALERT-BOX INFO BUTTONS OK.
DEFINE VARIABLE given-date AS DATE NO-UNDO .
PROMPT-FOR given-date.
DISP DAY(add-interval( date( month( INPUT given-date), 1, year( INPUT given-date)), 1, "month" ) - 1) label "Lastdate".
This Should do it.
DEFINE VARIABLE InputDate AS DATE NO-UNDO.
DEFINE VARIABLE OutputDate AS DATE NO-UNDO.
IF MONTH(InputDate) = 12 THEN
ASSIGN OutputDate = DATE(1,1,(YEAR(InputDate) + 1 ) ) - 1 .
ELSE
ASSIGN OutputDate = DATE((MONTH(InputDate) + 1 ), 1,YEAR(InputDate) ) - 1.
This solution should provide you the last date for any month.
DEFINE VARIABLE InputDate AS DATE NO-UNDO.
DEFINE VARIABLE OutputDate AS DATE NO-UNDO.
ASSIGN OutputDate = DATE(((MONTH(InputDate) MOD 12) + 1 ),1,YEAR(InputDate)) - 1.
© 2022 - 2024 — McMap. All rights reserved.