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).
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
Here is my favorite;
ASSIGN dtFirstOfMonth = DATE(MONTH(dtDate),1,YEAR(dtDate))
dtLastOfMonth = dtFirstOfMonth + 31 - DAY(dtFirstOfMonth + 31).
Used this forever :-)
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
That's a really nice solution Thomas!
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.
> 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?
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.