Convert date formats in ILE RPG *YMD, *CYMD, *DMY, *MDY. Any combination!
Date Format Conversion Made Easy!
As a new RPG programmer I have been looking for some elegant way to convert a date from one format to another. Most often than not, I have seen data structures being used for date format conversions.
However, there’s an easier, less trickier and comprehensible way for date format conversion. Before I tell you the elegant way, however, I must again repeat one thing; Dates do not have any format. They are just one of several data types available to us. So, when I say date format conversion, I actually mean numeric (sometimes character) fields being used to store some date value.
Steps of date conversion:-
To convert a date from one format to another we need to declare three variables as below.
- Two variables to store numeric date in from and to-date formats. Lengths of these variables should be exactly equal to the maximum possible length for that format.
For *MDY, *DMY, *YMD – 6 digits
For *CYMD – 7 digits
For *USA and *ISO – 8 digits.
- One date type variable which will temporarily store the date value.
How does it work?
Date conversion in RPG is a two step process as explained below.
First we convert the numeric date value into actual date data type value using the MOVE opcode. Now since we know that a date does not have any format, we can now treat this date field to contain the date as per our required format and hence we can again MOVE this date into our numeric variable in the desired format. Easy, isn’t it?
In the example given below, 31st Dec 2039 is converted from *YMD to *CYMD, *DMY, *MDY and *USA format. Idea of this example is to illustrate how easily numeric date values can be converted from format to another. This example can hence be extended to convert a date from any format to any other specific format.
Example to convert date formats in RPGLE
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++ D W@Date S D D W@YMD S 6 0 Inz(391231) D W@MDY S 6 0 Inz D W@DMY S 6 0 Inz D W@CYMD S 7 0 Inz D W@USA S 8 0 Inz D W@ISO S 8 0 Inz * CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++ C *YMD Move W@YMD W@Date C *MDY Move W@Date W@MDY C *DMY Move W@Date W@DMY C *CYMD Move W@Date W@CYMD C *USA Move W@Date W@USA C Move W@Date W@ISO * C W@ISO Dsply C W@MDY Dsply C W@DMY Dsply C W@CYMD Dsply C W@USA Dsply C Return