Blog Archive

RPGLE convert date to numeric or character - Use %date(), %Char(), %dec()

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

If your dates come in one of the 12 standard formats all you need to do is indicate the field containing your date and the format it is in and the %DATE BIF does the rest.

Click here for date formats


Most of our dates are stored as YYYYMMDD in 8-byte numeric fields. This is the numeric version of the ISO format. We can convert this to a date data type with the following code:

%date(num_date : *iso)

The "*iso" tells the %date BIF how my numeric date field (num_date) is formatted. We also have a few dates stored as MMDDYYYY in 8-byte numeric fields. In this case all I change is the format on the %date BIF:

%date(num_date : *usa)

For anyone storing dates in alpha fields, the format you specify will depend on whether or not you store the date separators. For example, if your date is in a 10-byte character field as YYYY-MM-DD (with the dashes), this is again ISO format:

%date(alpha_date : *iso)

If you do not store standard separators with your date (YYYYMMDD in an 8-byte character field) simply append the number zero (0) to the end of the format name:

%date(alpha_date : *iso0)

If you store valid separators with your date but not the default for the format (MM-DD-YY in an 8-byte character field), then the default separator is the slash (/) and you indicate the separator at the end of the format name:

%date(alpha_date : *mdy-)

As you can see, as long as you have your dates in one of the 12 standard formats, it's pretty easy to convert it to a date data type.

Now that you have your date as a date data type, it's time to convert it to the format of your choice. To convert a date data-type to an alpha format, use the %CHAR BIF and specify the format that you want to receive. For example, if you want to create a character date as MM/DD/YYYY, specify:

%char(date_fld : *usa)

If you want the same date but no separators:

%char(date_fld : *usa0)

The nice thing about using BIFs (and especially in free format) is that you don't need to do separate steps to get your result. Instead, just do it all in one step. If I want to convert from an 8-byte numeric YYYYMMDD date to a 10-character alpha MM/DD/YYYY date I can do it in one statement:

%char( %date(num_date : *iso) : *usa0) //Output MMDDYYYY

%char( %date(num_date : *iso) : *usa) //Output MM/DD/YYYY
Remember that the inner conversion takes place first (%date), followed by the outer conversion (%char). The format specified on the %date BIF indicates what you are converting from; the format specified on the %char BIF indicates what you are converting to.

Converting to a numeric field requires just one additional step: convert the alpha date to numeric using the %dec BIF:

%dec( %char( %date(num_date : *iso) : *usa0) : 8 : 0)

Be sure to specify the correct length, in this case 8-bytes for MMDDYYYY and zero (0) decimal positions. That's how easy it is to convert from any standard format to any other standard format.

Sample Cheat sheets


// date to numeric...
$num_A = %dec(%char($date_A:*ymd/):6:0); // D'ccyy-mm-dd' to yymmdd 
$num_B = %dec(%char($date_A:*cymd/):7:0); // D'ccyy-mm-dd' to cyymmdd 
$num_C = %dec(%char($date_A:*iso-):8:0); // D'ccyy-mm-dd' to ccyymmdd 
$num_D = %dec(%char($date_A:*mdy/):6:0); // D'ccyy-mm-dd' to mmddyy 
$num_E = %dec(%char($date_A:*usa/):8:0); // D'ccyy-mm-dd' to mmddccyy 

// date to character...
$char_A = %char($date_A:*ymd/); // D'ccyy-mm-dd' to 'yy/mm/dd' 
$char_B = %char($date_A:*usa/); // D'ccyy-mm-dd' to 'mm/dd/ccyy' 
$char_C = %char($date_A:*mdy/); // D'ccyy-mm-dd' to 'mm/dd/yy' 

// character to numeric...
$num_A = %dec(%char(%date($char_A:*ymd/):*ymd0):6:0); // 'yy/mm/dd' to yymmdd 
$num_B = %dec(%char(%date($char_A:*ymd/):*cymd0):7:0); // 'yy/mm/dd' to cyymmdd 
$num_C = %dec(%char(%date($char_A:*ymd/):*iso0):7:0); // 'yy/mm/dd' to ccyymmdd 
$num_D = %dec(%char(%date($char_A:*ymd/):*mdy0):7:0); // 'yy/mm/dd' to mmddyy 
$num_E = %dec(%char(%date($char_A:*ymd/):*usa0):7:0); // 'yy/mm/dd' to mmddyyyy 
$num_A = %dec(%char(%date($char_B:*usa/):*ymd0):6:0); // 'mm/dd/ccyy' to yymmdd 
$num_B = %dec(%char(%date($char_B:*usa/):*cymd0):7:0); // 'mm/dd/ccyy' to cyymmdd
$num_C = %dec(%char(%date($char_B:*usa/):*iso0):7:0); // 'mm/dd/ccyy' to ccyymmdd
$num_D = %dec(%char(%date($char_B:*usa/):*mdy0):7:0); // 'mm/dd/ccyy' to mmddyy 
$num_E = %dec(%char(%date($char_B:*usa/):*usa0):7:0); // 'mm/dd/ccyy' to mmddyyyy
$num_A = %dec(%char(%date($char_C:*mdy/):*ymd0):6:0); // 'mm/dd/yy' to yymmdd 
$num_B = %dec(%char(%date($char_C:*mdy/):*cymd0):7:0); // 'mm/dd/yy' to cyymmdd 
$num_C = %dec(%char(%date($char_C:*mdy/):*iso0):7:0); // 'mm/dd/yy' to ccyymmdd 
$num_D = %dec(%char(%date($char_C:*mdy/):*mdy0):7:0); // 'mm/dd/yy' to mmddyy 
$num_E = %dec(%char(%date($char_C:*mdy/):*usa0):7:0); // 'mm/dd/yy' to mmddyyyy

More Sample Date conversion Cheat Sheets


No comments :

Post a Comment