Using RPG IV (RPG ILE) Data Structure For Date Conversion

Convert *ISO date into *CYMD numeric format.

DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++
D CYMD            DS                                     
D Cent                    1      2  0                    
D CYMDYr                  3      4  0                    
D CYMDMn                  5      6  0                    
D CYMDDy                  7      8  0                    
D CYMDDate                1      8  0                    
 *                                                       
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++
D ISO             DS                                     
D ISODate                 1     10D   Inz(d'2006-06-07') 
D ISOYr                   1      4  0                    
D ISOYr2                  3      4  0                    
D ISOMn                   6      7  0                    
D ISODy                   9     10  0                    
 *                                                       
CL0N01Factor1+++++++Opcode&ExtExtended-factor2++++++++
C                   Eval      CYMDYr = ISOYr2         
C                   Eval      CYMDMn = ISOMn          
C                   Eval      CYMDDy = ISODy          
C                   If        ISOYr  >= 2000          
C                   Eval      Cent = 1                
C                   Else                              
C                   Eval      Cent = 0                
C                   EndIf                             
 *                                                    
C     ISODate       Dsply                             
C     CYMDDate      Dsply                             
C                   Return                            

To convert a date from one format to another we first define data structures corresponding to the formats. In the above example, we have to convert an ISO date into CYMD format. For this we first define two data structures corresponding to the fields. Now we move the year, month and day fields of the ISO data to their respective CYMD data structure fields. Finally we calculate the century of the dates.

That’s it.

Note:- Here, CYMDDate is not of date data type. However, since many programmers use dates in numeric format only hence this conversion. Fact is that date format conversion is for display purpose only. This is because date in itself is stored as binary data type. And it’s absolutely perfect to subtract an *CYMD date from *ISO date.

Similar to the above example we could use data structures to convert an *ISO date into *YMD, *MDY, *DMY and *USA format.