Midrange News for the IBM i Community


Posted by: Bob Cozzi
Rogue Programmer
Cozzi Productions, Inc.
Chicagoland
Get 1st of Month and End of Month in RPGIV
has no ratings.
Published: 19 Sep 2012
Revised: 23 Jan 2013 - 1640 days ago
Last viewed on: 21 Jul 2017 (3464 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Get 1st of Month and End of Month in RPGIV Published by: Bob Cozzi on 19 Sep 2012 view comments(6)

Calculating the 1st of Month in RPG IV

beginMonth = myDate - %Days(%subdt( myDate:*days)-1);  

Calculating the End of Month in RPG IV

endMonth = (myDate + %Months(1)) - %days(%subdt(myDate + %Months(1):*days));

 Test Program that Shows Results

.....H bnddir('COZTOOLS/COZTOOLS')                                              
     H DFTACTGRP(*NO) ACTGRP(*NEW)                                              
                                                                                
     D/include cozTools/qcpysrc,joblog                                          
                                                                                
     D myDate          S               D   Inz(*SYS)                            
                                                                                
     D BOM             S               D                                        
     D EOM             S               D                                        
     C                   MOVE      *ON           *INLR                          
      /free                                                                     
           BOM = myDate - %Days(%subdt( myDate:*days)-1);                       
           EOM = (myDate+%Months(1)) - %days(%subdt(myDate + %Months(1):*days));
           joblog('myDate: %s BOM: %s EOM: %s':                                    
                     %char(myDate:*USA):                                        
                     %char(BOM:*USA):                                           
                     %char(EOM:*USA));                                          
      /end-free                                              

 

 

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: DaleB
Premium member *
Reading, PA
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 4 years 9 months 30 days 6 hours 56 minutes ago

Performance question: Is the compiler smart enough to recognize that the repeated subexpression in endMonth has no side effects, and therefore can be reused, or would it be better to explicitly do the intermediate calc with a work field?

wrkDat   = myDate + %Months(1);
endMonth = wrkDat - %days(%subdt(wrkDat:*days));

 

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 4 years 9 months 30 days 4 hours 34 minutes ago

I would personally do the extra step myself because then its going to use the variable no matter what. RPG IV's optimizer does however optimize simple redundancies like this specific one. So I think it wouldn't really matter.

Posted by: Ringer
Premium member *
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 4 years 9 months 27 days 9 hours 38 minutes ago

endMonth = BeginMonth + %Months(1)  - %Days(1) ;

Chris Ringer

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 4 years 9 months 27 days 6 hours 12 minutes ago

Chris, I actually have/had that in my original example, however it doesn't stand-alone for a given date. You'd have to first calculate the 1st of the month, then pass that date to the EOM routine. So I made it more stand-alone.

Posted by: Ringer
Premium member *
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 4 years 9 months 27 days 4 hours 2 minutes ago

Well hey, I know you'd make those sub-procedures so... Cozzi_BeginMonth() would be available anytime. Laughing

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Get 1st of Month and End of Month in RPGIV
Posted: 4 years 9 months 27 days 1 hours 46 minutes ago

As would getEndOfMonth()