RPGLE convert numeric to date

To populate a date variable from something other than a literal string, you have to use the IBM-supplied %date BIF. If used with no parameters, %date will return the current system date.


d myDate           s               d    

 /free
    myDate = %date();
    // myDate = *the current system date*
    *inlr = *on ;
 /end-free
Imagine you have a numeric variable containing a number representing a date in a YYYYMMDD format:
d myDate8         s              8  0 inz(20040501)
In its numeric version, this is *ISO format, so we can create our date like so:
/free
    myDate = %date( myDate8 );
 /end-free

Now we have a "real" date field populated with the equivalent of “May 1, 2004”, but if our numeric value was in a different format this wouldn't work. In this case we need to inform the %date BIF what the format of the incoming numeric should correspond to:
d myDate          s               d
d myDate8         s              8  0 inz(05012004)

 /free
    myDate = %date( myDate8 : *USA );
    dsply myDate ;

    *inlr = *on ;
 /end-free
If you compile and run this, you will see that we still get our output in the *ISO format. This is because we did not change the DATFMT of the myDate variable; we only instructed the %date BIF to expect the incoming parameter in the *USA format.

So far we've focused on date formats with four-digit years. While ideally we would all use four-digit years all the time, this isn't very realistic, since there are still a lot of six-digit numeric dates floating around out there pretending to be "real" dates. Not to worry, %date can handle these as well, given that you supply the appropriate format name.
d myDate          s               d
d myDate6         s              6  0 inz(050104)

 /free
    myDate = %date( myDate6 : *MDY );
    dsply myDate ;

    *inlr = *on ;
 /end-free
Of course, 050104 can just as easily be interpreted as *YMD or *DMY. Compile and run the following snippet:
d myDate          s               d
d myDate6         s              6  0 inz(050104)

 /free
    myDate = %date( myDate6 : *MDY );
    dsply myDate ;
    myDate = %date( myDate6 : *DMY );
    dsply myDate ;
    myDate = %date( myDate6 : *YMD );
    dsply myDate ;
    *inlr = *on ;
 /end-free
And you get the following results:
DSPLY  2004-05-01                                                           
DSPLY  2004-01-05                                                           
DSPLY  2005-01-04
Three different dates from the same variable. The lesson here, of course, is to be very cautious with six-digit fields. The other thing to consider is that the valid date range with any two-digit-year date format is limited to a range of years from 1940 to 2039. While this may not seem like a problem, the default for any date field is 0001-01-01, which is out of the range of valid two-digit-year dates.
Source: ITjungle by by Joel Cochran Read More ...

Cheat Sheets for Numeric data to Date

D @dateA          S               d              
                                                                       
D @numA           S              6  0 inz(041205)                      
D @numB           S              7  0 inz(1041206)                     
D @numC           S              8  0 inz(20041207)                    
D @numD           S              6  0 inz(120804)                      
D @numE           S              8  0 inz(12092004)
/free 
  @dateA = %date(@numA:*ymd);        // yymmdd         to  D'ccyy-mm-dd' 
  @dateA = %date(@numB:*cymd);       // cyymmdd        to  D'ccyy-mm-dd' 
  @dateA = %date(@numC:*iso);        // ccyymmdd'      to  D'ccyy-mm-dd' 
  @dateA = %date(@numD:*mdy);        // mmddyy         to  D'ccyy-mm-dd' 
  @dateA = %date(@numE:*usa);        // mmddccyy       to  D'ccyy-mm-dd'
/end-free

Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.