Return to the RPG Tips
How do I compute the last day of the month?
The month has always been the most frustrating component of date arithmetic. Is it a 30- day month? A 31-day month? Is it February? In a leap year? The code below calculates the last day of the month in which any date (DateIn) occurs. The logic is so simple that it's easy to overlook: The first day of the following month is always the 1st, and the day before that is the last day of the month. This code adds a month to the date, changes the day to the first, then subtracts a single day. Voil? ... DateEnd will be the last day of the month. ********************************************************************** * * LastDay Determines last day of month for a date * ********************************************************************** D DateIn S D D DS D DateEnd D DATFMT(*ISO) D DateDay 2 OVERLAY(DateEnd:9) C DateIn ADDDUR 1:*M DateEnd C EVAL DateDay = '01' C SUBDUR 1:*D DateEnd
[report a broken link by clicking here]