Problem solve Get help with specific problems with your technologies, process and projects.

Convert a date from MMDDYY to YYMMDD -- revisited

Learn how RPG allows redefinition of data within accessible data structures.

Regarding the tip, Where does the 01-01-0001 default date come from?, the solution given by John Kohan, while accurate, is extremely wasteful of processing cycles. Coming from a commercial and world-wide environment where end-period processing time is extremely limited, anything that slows down batch processing is not popular.

The proper solution keeps coming up again and again, but for some reason people just don't get it. Unlike many other languages, RPG allows redefinition of data within accessible data structures. So, to convert a date from MMDDYY to YYMMDD, use:

I               DS 
I                           1     2 YY2 
I                           1     60TODAT 
I                           3     80FRDAT 
I                           7     8 YY1 

C               MOVE YY1       YY2 

That's it! I am assuming here that FRDAT comes from a record, so it fills its part of the data structure after a READ. TODAT contains the date in YYMMDD format after the MOVE.

This method uses far fewer processing cycles than an arithmetic method. Most programmers with more than 10 years' experience know it, and we generally try to save cycles when we can -- and where the method is transparent. The data structure method of date conversion is obvious by looking at it (though comments never hurt), but the arithmetic method is not obvious, and requires commenting wherever used.

By the way, a contractor at my place the other day used the following EVAL to create a key value (8-digit CCYYMMDD value):

Eval       DateKey = (PerYear * (10 ** 4)) + (PerMonth * (10 ** 2)) + PerDay 

You can work out your own data structure solution! 


The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

Dig Deeper on iSeries ILE programming

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.