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.
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.
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
DSPLY 20391231
DSPLY 123139
DSPLY 311239
DSPLY 1391231
DSPLY 12312039
Recent comments
5 days 1 hour ago
5 days 2 hours ago
5 days 2 hours ago
5 days 4 hours ago
5 days 15 hours ago
5 days 17 hours ago
5 days 18 hours ago
5 days 19 hours ago
6 days 8 hours ago
6 days 18 hours ago