7

How can I get the last day of the month in progress 4gl?

Daria Trainor
  • 5,545
  • 4
  • 23
  • 30

10 Answers10

15
/* 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.
Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
2
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
Sagar V
  • 12,158
  • 7
  • 41
  • 68
1

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)
    .
Tim Kuehn
  • 3,201
  • 1
  • 17
  • 23
1
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.
j0k
  • 22,600
  • 28
  • 79
  • 90
Wander
  • 11
  • 1
1

The easiest way is to use ADD-INTERVAL function.

ldEndDate = ADD-INTERVAL(ldBeginDate, 1, "months") - 1.
user3790173
  • 45
  • 1
  • 6
0
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.
Daria Trainor
  • 5,545
  • 4
  • 23
  • 30
0

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.
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Carlos
  • 21
  • 1
0

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.
0

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".

Purushottam
  • 35
  • 1
  • 8
0

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.
Mani
  • 1
  • 4