Determine last day of previous month

45 pts.
Can you help me in determining the last day of the previous month. For example today it is 2008.06.19, the rpgle-routine would return 2008.05.31 Thanks

Answer Wiki

Thanks. We'll let you know when a new response is added.

I don’t know te in RPG, but the typicall way to do it, is to find the first day of the current month, then subtract one day. I’m a database guy, so if I was doing this in T/SQL I’d use something like this.

<pre>DECLARE @MyDate datetime
SET @MyDate = DATEADD(dd, DATEPART(dd, getdate())*-1, GETDATE())
SET @MyDate = DATEADD(dd, -1, @MyDate)</pre>

Or a more streamlined version.

<pre>DECLARE @MyDate datetime
SET @MyDate = DATEADD(dd, (DATEPART(dd, getdate())*-1)-1, GETDATE())</pre>

Have 2 Arrays one MON & another DAYS
Lookup MON with Current system Month
and use same index to identify DAYS


The process will be:

1. get the month from the date (thru substring or buit-in function)
2.Check in the array the total no of days for that (month-1).
3.create a date with MMDDYY where MM is the last month,DD is the max days for that month ,YY is the same year
4.If MM is 01 then subtract (YY-1)


Simplest way is perhaps the LAST_DAY(current_date – 1 month) SQL function. It requires a current release.

In RPGLE (free-format)
<pre>endPriorMonth = %date() – %days(%subdt(%date():*days)) -%days(1);</pre>

Discuss This Question: 6  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Teckgeck
    the answer is basically right - here is the RPG as you can see - manuipulate the date in the data structure so that the current date is changed to the first of the current month - then subtract 1 day DTODAY DS D TODAYD D /FREE TODAYD = %DATE(); %SUBST(TODAY:9:2) = '01'; TODAYD-=%DAYS(1); *INLR = *ON; /END-FREE
    170 pointsBadges:
  • Gilly400
    Hi, The last day of the month is in essence a constant dependent on which month it is and whether or not it's a leap year, so you could hard-code for this :-
         C     CurrentMonth  Sub       1             LastMonth         2 0
         C                   Select                                       
         C     LastMonth     WhenEq    1                                  
         C     Year          Div       4             Dummy             5 0
         C                   Mvr                     LeapYear          1 0
         C     LeapYear      IfNe      *Zeros                             
         C                   Z-add     28            DayNumber            
         C                   Else                                         
         C                   Z-add     29            DayNumber            
         C                   Endif                                        
         C     LastMonth     WhenEq    1                                  
         C     LastMonth     Oreq      3                                  
         C     LastMonth     Oreq      5                                  
         C     LastMonth     Oreq      7                                  
         C     LastMonth     Oreq      8                                  
         C     LastMonth     Oreq      10                                 
         C     LastMonth     Oreq      12                                 
         C                   Z-add     31            DayNumber            
         C     LastMonth     WhenEq    4                                  
         C     LastMonth     Oreq      6                                  
         C     LastMonth     Oreq      9                                  
         C     LastMonth     Oreq      11                                 
         C                   Z-add     30            DayNumber            
         C                   EndSl      
    This isn't a perfect solution (if you can divide the year by 4, it's not *always* a leap year), but it should work for most situations. Regards, Martin Gilbert.
    23,730 pointsBadges:
  • Gilly400
    Whoops, That first "C LastMonth WhenEq 1" should be "C LastMonth WhenEq 2", but then you guys already knew that, right. Regards, Martin.
    23,730 pointsBadges:
  • BigKat
    if you always want the previous month-end date whenever you run the program, use this:
    d wrkdate         s               d                    
          wrkdate = %date() - %days(%subdt(%date():*days));
          *inlr = *on;                                     
    otherwise, replace %date() with the date you want to find the previous month-end date for. Kevin C. Ketzler - Affiliated
    8,710 pointsBadges:
  • Jbmm
    basically, it's the first day of current month - 1 day always right without the coding, in easy words: date (01 + current month + current year) - 1 day
    125 pointsBadges:
  • Cwc
    I think the the latter two solutions are preferrable since they utilize RPG IV's built in date handling, which leaves less room for error. BigKat, your way looks pretty bullet proof. Regarding leap years, for anyone who's interested, I recommend going beyond the simple divide by 4 logic, as it will not work in the year 2100. Yes, few of us here today will be around then, but similar logic will likely still be in use in whatever form it exists then. I know there will be plenty of false leap year determinations in 2100. The additional rule is that for years ending in 00, they must be evenly divisible for 400 in order to be a leap year. Thus, 2000 was a leap year, but 1900 was not, and 2100, 2200, and 2300 will not be.
    4,290 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: