Blog Archive

RPGLE convert date format from one to another

In this tutorial we are going to learn convert various date formats such as *JUL, *MDY, *DMY, *YMD, *CYMD, *CDMY, *CMDY, *ISO, *USA, *EUR, *JIS, *LONGJUL, etc. To do the date format conversion we are going to take help of the three built-in-function or BIFs %date(), %char() and %dec(). Click here to learn valid RPGLE date formats

%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

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(05182011)

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

    *inlr = *on ;
 /end-free

%char()


%CHAR(expression{:format})

%CHAR converts the value of the expression from graphic, UCS-2, numeric, date, time or timestamp data to type character. The converted value remains unchanged, but is returned in a format that is compatible with character data.

%dec()

the %DEC function converts a string expression to a packed decimal number, with a specified precision:

%DEC(expression:digits:decimals)


Step 1 - Convert the data into a date

Input data is Numeric
$date_A = %date($num_A:*ymd);
// Output_date = %date(Numeric_Input:Input_format) 

Input data is Character
$date_A = %date($char_A:*ymd/);
// Output_date = %date(Charater_Input:Input_format and separator)

Step 2 - Convert to another format using %char()
$char_B = %char($date_A:*usa/);
// Character_output = %date(Date_Input:Output_format and separator)

Step 3 - If need the output to be numeric then use %dec()
$num_E = %dec(%char($date_A:*usa/):8:0);
// Numeric_output = %dec(Character_Input:length:0)

Sample Cheat sheets for date conversion

// numeric to numeric...
$num_B = %dec(%char(%date($num_A:*ymd):*cymd0):7:0); // yymmdd to cyymmdd
$num_C = %dec(%char(%date($num_A:*ymd):*iso0):8:0); // yymmdd to ccyymmdd
$num_D = %dec(%char(%date($num_A:*ymd):*mdy0):6:0); // yymmdd to mmddyy
$num_E = %dec(%char(%date($num_A:*ymd):*usa0):8:0); // yymmdd to mmddccyy
$num_A = %dec(%char(%date($num_B:*cymd):*ymd0):6:0); // cyymmdd to yymmdd
$num_C = %dec(%char(%date($num_B:*cymd):*iso0):8:0); // cyymmdd to ccyymmdd
$num_D = %dec(%char(%date($num_B:*cymd):*mdy0):6:0); // cyymmdd to mmddyy
$num_E = %dec(%char(%date($num_B:*cymd):*usa0):8:0); // cyymmdd to mmddccyy
$num_A = %dec(%char(%date($num_C:*iso):*ymd0):6:0); // ccyymmdd to yymmdd
$num_B = %dec(%char(%date($num_C:*iso):*cymd0):7:0); // ccyymmdd to cyymmdd
$num_D = %dec(%char(%date($num_C:*iso):*mdy0):6:0); // ccyymmdd to mmddyy
$num_E = %dec(%char(%date($num_C:*iso):*usa0):8:0); // ccyymmdd to mmddccyy
$num_A = %dec(%char(%date($num_D:*mdy):*ymd0):6:0); // mmddyy to yymmdd
$num_B = %dec(%char(%date($num_D:*mdy):*cymd0):7:0); // mmddyy to cyymmdd
$num_C = %dec(%char(%date($num_D:*mdy):*iso0):8:0); // mmddyy to ccyymmdd
$num_E = %dec(%char(%date($num_D:*mdy):*usa0):8:0); // mmddyy to mmddccyy
$num_A = %dec(%char(%date($num_E:*usa):*ymd0):6:0); // mmddccyy to yymmdd
$num_B = %dec(%char(%date($num_E:*usa):*cymd0):7:0); // mmddccyy to cyymmdd
$num_C = %dec(%char(%date($num_E:*usa):*iso0):8:0); // mmddccyy to ccyymmdd
$num_D = %dec(%char(%date($num_E:*usa):*mdy0):6:0); // mmddccyy to mmddyy

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

Recommended Reading


No comments :

Post a Comment