Converting a numeric to a date in MM/DD/YYYY format in IBM I RPG

Viewed 564

I'm working on a project where I have an external file with dates in a numeric format (like: 20,210,807) rather than in date format. I need to get them into a report formatted in MM/DD/YYYY. I have a data structure started but I'm at a stalemate on what I need to do next. I know I need to bring the ODATE field into my date structure but I'm having a mental roadblock on how to accomplish that. I am using Free Format instead of Fixed. The code is below.

   Dcl-ds PrintLine ExtName('CSORDPL3':*all);      // Declared data structure using logical file
      DORDNBR  ExtFld('ORDNBR');                   // Order number from logical file
      DCUSTNO  ExtFld('CUSTNO');                   // Customer number from logical file
      DCFNAME  ExtFld('CFNAME');                   // First name from logical file
      DCLNAME  ExtFld('CLNAME');                   // Last name from logical file
      DCPHONE  ExtFld('CPHONE');                   // Phone number from logical file
      DORDTOT  ExtFld('ORDTOT');                   // Order total from logical file
      DODATE   ExtFld('ODATE');                    // Order date from logical file
   End-Ds;

   Dcl-ds Datefield;      // Data structure for Date field to reformat numerical
       MM Zoned(2:0);
       DD Zoned(2:0);
       YY Zoned(4:0);
   END-DS;

'''

2 Answers

first use the %char built in function to convert the numeric format date field to character. Then the %date function to convert from character field to date field. Then use %char on the date field to convert it to a mm/dd/yyyy formatted date.

and use monitor to handle any invalid date input values.

d numDate         s              8p 0            
d crtDate         s               d              
d formattedDate   s             10a              
 /free                                           
      numDate     = 20210803 ;                   
      monitor ;                                  
      crtDate     = %date(%char(numdate):*iso0) ;
      on-error ;                                 
      crtdate     = *loval ;                     
      endmon ;                                   
      formattedDate  = %char(crtDate:*usa) ;        
      dsply       formattedDate ;     

Declaring the work variable as Dcl-s wDate date(*USA)
and using %date(20210708) should work.

Related