Find FIRST DAY AND LAST DAY OF LAST MONTH

Posted by hikmetalemdaroglu@gmail.com on 18-Mar-2016 18:36

This is a simle procedure it works fine. 

/* --------------------------------------- *

FIRST DAY AND LAST DAY OF LAST MONTH

HIKMET ALEMDAROGLU
hikmetalemdaroglu@gmail.com

www.kronosoft.com.tr

* ---------------------------------------- */

DEF INPUT PARAM PR-INDATE AS DATE format "99/99/9999" NO-UNDO. /* MM/DD/YYYY */
DEF OUTPUT PARAM PR-PREVDATE_OF_FIRST_DAY AS DATE format "99/99/9999" NO-UNDO.
DEF OUTPUT PARAM PR-PREVDATE_OF_LAST_DAY AS DATE format "99/99/9999" NO-UNDO.

ASSIGN PR-PREVDATE_OF_FIRST_DAY = PR-INDATE - (DAY(PR-INDATE) + DAY(PR-INDATE - DAY(PR-INDATE))) + 1
PR-PREVDATE_OF_LAST_DAY = PR-INDATE - DAY(PR-INDATE).

All Replies

Posted by OctavioOlguin on 18-Mar-2016 23:22

I use the following to find the first and last day of any month, given a date inside the month

DEFINE INPUT PARAMETER  pDate AS DATE NO-UNDO.
DEFINE OUTPUT PARAMETER pDate1 AS DATE NO-UNDO.
DEFINE OUTPUT PARAMETER pDate31 AS DATE NO-UNDO.

IF month(pDate) = 12 THEN
   ASSIGN 
          pDate31  = DATE(MONTH(pDate), 31, YEAR(pDate))
          pdate1   = DATE(MONTH(pDate), 1, YEAR(pDate)).
ELSE
   ASSIGN 
          pDate31  = DATE(MONTH(pDate) + 1, 1,  YEAR(pDate)) - 1
          pdate1   = DATE(MONTH(pDate), 1, YEAR(pDate)).

Greetings

Posted by Thomas Wurl on 19-Mar-2016 04:05

Here is my favorite;

 ASSIGN dtFirstOfMonth = DATE(MONTH(dtDate),1,YEAR(dtDate))

                 dtLastOfMonth  = dtFirstOfMonth + 31 - DAY(dtFirstOfMonth + 31).

Used this forever :-)

Posted by Alon Blich on 19-Mar-2016 04:22

you may want to have a look at the slibdate.p library, in the standard libraries project at the oehive.org.

{slib/slibdate.i}

message date_getMonthStart( 1980, 2 ) date_getMonthEnd( 1980, 2 ).

message date_getMonthStart( 1980, 2 + 48 ) date_getMonthEnd( 1980, 2 + 48 ).

hth

Posted by Grant Holman on 22-Mar-2016 06:34

That's a really nice solution Thomas!

Posted by Jens Dahlin on 22-Mar-2016 08:49

I would leave it to the ABL. Add a month and remove a day.

PROCEDURE getFirstAndLastDate:

    DEFINE INPUT PARAMETER  pdaDate AS DATE NO-UNDO.
    DEFINE OUTPUT PARAMETER pdaFirstDate AS DATE NO-UNDO.
    DEFINE OUTPUT PARAMETER pdaLastDate  AS DATE NO-UNDO.

    ASSIGN 
        pdaFirstDate = DATE( MONTH(pdaDate), 1, YEAR(pdaDate))
        pdaLastDate  = ADD-INTERVAL(pdaFirstDate, 1, "months") - 1.

END.

Posted by George Potemkin on 22-Mar-2016 11:06

> ADD-INTERVAL(pdaFirstDate, 1, "months")

or

ADD-INTERVAL(pdaFirstDate, 1, "month")

A bit off-topic: the interval units in the singular form are not documented but can be used. Should we use them?

Posted by Matt Gilarde on 22-Mar-2016 11:11

You can safely use them in singular or plural form. There are two lists in the code (singular and plural) and the units parameter is checked against both.

This thread is closed