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    

    myDate = %date();
    // myDate = *the current system date*
    *inlr = *on ;

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


  1. This is great, you are good, i like your post and i still waiting our next post
    Bandar Ceme

  2. Absolutely really Amazing Articel and good point in there.
    Thanks shared.


  3. Apple's flagship operating system iOS 11 is nearing it's release.
    There will be quite a few changes from iOS 10 as well as some compatibility updates. You can check out my site to get all the details about iOS 11, the compatible devices, release date and
    iOS 11 download

  4. I do love to play xbox games and when i got a website which can deliver free codes for xbox gaming. Now I rule the world of xbox games over Internet. Its wonderful to visit xbox live codes .Visit the website to get fre xbox codes and membership for the same. Its the time to exploier xbox gaming. Thanks for the website.

  5. Wow! I’m from Malaysia, an English teacher gonna be. Now, I know that my goal readers are my students in the past. custom essay writing service I’d like to mark like this too attractive event and real hard work to make an enormous article. Thank you for the suggestions, I really enjoyed reading this. It is so much inspiring.

  6. Activate device microphone for tracking of ambient sound and here the surroundings where the target device is located, have a glance at this link

  7. The information which you people are given are really good, These look really great.

  8. This is really very nice blog and so informative. Thanks a lot for sharing this article.

  9. Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.
    - twitter search

  10. Thanks for the information you brought to us. They are very interesting and new. Look forward to reading more useful and new articles from you!

  11. Thanks for posting, this is really amazing.
    BandarQQ Online

  12. Some of these information are really amazing. Thank you for giving me good information.
    Situs BandarQQ

  13. I would like to thank you for all the information you give. information your is useful to me.
    BandarQQ Terpercaya

  14. Thanks for the information your article brings. I see the novelty of your writing, I will share it for everyone to read together. I look forward to reading many articles from you.