%DATE Built-In Functions in rpgle
It is represented as %DATE { value { : date-format }
It is used to convert a character, numeric, or timestamp data to Date type.
In %Date() function, 1st parameter is the Input value to be converted to date.
In %Date() function, 2nd parameter is the Input Date Format. So, if we mention date-format as *YMD, system treats the Input value in that format.
e.g.
If we enter Input value as 170501, system treats this as 17/05/01 and returns the output in *ISO for this date value i.e. 2017-05-01
The output is returned in *ISO format by default. If we want the output to be in different format we need to declare that date format for Output variable in D-Spec.
Few of the frequently used DATE FORMATS are given below:
*YMD – YY/MM/DD
*DMY – DD/MM/YY
*ISO – YYYY-MM-DD
*USA – MM/DD/YYYY
%date function Example(1) - Get today's Date
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 d Today s d
0002.00
0003.00 /free
0004.00 Today = %date(); // Today = Current system date
0005.00
0006.00 DSPLY Today;
0007.00 /end-free
0008.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 2017-11-12
%date function Example(2) - Convert numeric value to Date
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 D myDate s d
0002.00 D wDate s 8 0 inz(20170501)
0003.00
0004.00 /free
0005.00 myDate = %date( wDate );
0006.00 DSPLY myDate;
0007.00 /end-free
0008.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 2017-05-01
%date function Example(3) - Convert numeric value to Date
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 D myDate1 s d datfmt(*USA)
0002.00 D myDate2 s d datfmt(*ISO)
0003.00 D wDate1 s 8 0 inz(01032017)
0004.00 D wDate2 s 8 0 inz(20170301)
0005.00
0006.00 /free
0007.00 myDate1 = %date( wDate1: *USA );
0008.00 // Here %date converts wDate1(MMDDYYYY) into *USA(MM/DD/YYYY)
0009.00
0010.00 myDate2 = %date( wDate2: *ISO );
0011.00 // Here %date converts wDate2(YYYYMMDD) into *ISO(YYYY-MM-DD)
0012.00
0013.00 DSPLY myDate1;
0014.00 DSPLY myDate2;
0015.00 /end-free
0016.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 01/03/2017
DSPLY 2017-03-01
%date function Example(4) - Convert numeric value to Date
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 D myDate1 s d
0002.00 D myDate2 s d
0003.00 D myDate3 s d
0004.00 D wDate s 6 0 inz(030117)
0005.00
0006.00 /free
0007.00 myDate1 = %date( wDate : *MDY );
0008.00 // Here %date converts wDate 03 01 17 (MMDDYY) into *ISO(YYYY-MM-DD)
0009.00
0010.00 myDate2 = %date( wDate : *DMY );
0011.00 // Here %date converts wDate 03 01 17 (DDMMYY) into *ISO(YYYY-MM-DD)
0012.00
0013.00 myDate3 = %date( wDate : *YMD );
0014.00 // Here %date converts wDate 03 01 17 (YYMMDD) into *ISO(YYYY-MM-DD)
0015.00
0016.00 DSPLY myDate1 ;
0017.00 DSPLY myDate2 ;
0018.00 DSPLY myDate3 ;
0019.00
0015.00 /end-free
0016.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 2017-03-01
DSPLY 2017-01-03
DSPLY 2003-01-17
%date function Example(5) - Convert char into Date
Columns . . . : 6 80 AMIT/QRPGSRC
SEU==> TESTRPG
FMT * *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
*************** Beginning of data *****************************************
0001.00 D myDate1 s d
0002.00 D myDate2 s d
0003.00 D myDate3 s d
0004.00 D wDate s 6A inz('030117')
0005.00
0006.00 /free
0007.00 myDate1 = %date( wDate : *MDY0 );
0008.00 // Here %date converts wDate 03 01 17 (MMDDYY) into *ISO(YYYY-MM-DD)
0009.00
0010.00 myDate2 = %date( wDate : *DMY0 );
0011.00 // Here %date converts wDate 03 01 17 (DDMMYY) into *ISO(YYYY-MM-DD)
0012.00
0013.00 myDate3 = %date( wDate : *YMD0 );
0014.00 // Here %date converts wDate 03 01 17 (YYMMDD) into *ISO(YYYY-MM-DD)
0015.00
0016.00 DSPLY myDate1 ;
0017.00 DSPLY myDate2 ;
0018.00 DSPLY myDate3 ;
0019.00
0015.00 /end-free
0016.00 C Seton LR
****************** End of data ********************************************
Output
DSPLY 2017-03-01
DSPLY 2017-01-03
DSPLY 2003-01-17