Date Data Type

Date fields have a predetermined size and format. They can be defined on the definition specification. Leading and trailing zeros are required for all date data.

Date constants or variables used in comparisons or assignments do not have to be in the same format or use the same separators. Also, dates used for I/O operations such as input fields, output fields or key fields are also converted (if required) to the necessary format for the operation.

Start of changeThe default internal format for date variables is *ISO. This default internal format can be overridden globally by the control specification keyword DATFMT, temporarily changed by /SET and /RESTORE directives, and individually set by the definition specification keyword DATE or DATFMT.End of change

The hierarchy used when determining the internal date format and separator for a date field is
  1. Start of changeFrom the DATE or DATFMT keyword specified on the definition specificationEnd of change
  2. Start of changeFrom the most recent /SET directive containing a DATFMT keyword that has not been restored by a /RESTORE directive.End of change
  3. From the DATFMT keyword specified on the control specification
  4. *ISO
There are three kinds of date data formats, depending on the range of years that can be represented. This leads to the possibility of a date overflow or underflow condition occurring when the result of an operation is a date outside the valid range for the target field. The formats and ranges are as follows:
Number of Digits in Year Range of Years
2 (*YMD, *DMY, *MDY, *JUL) 1940 to 2039
3 (*CYMD, *CDMY, *CMDY) 1900 to 2899
4 (*ISO, *USA, *EUR, *JIS, *LONGJUL) 0001 to 9999

Table 1 lists the RPG-defined formats for date data and their separators.

Note: The separator '&' indicates that a blank is used as the separator. For example, a date field defined with DATE(*YMD&) will have blanks as the separators.
For examples on how to code date fields, see the examples in:
Table 1. RPG-defined date formats and separators for Date data type
Format Name Description Format (Default Separator) Valid Separators Length Example
2-Digit Year Formats
*MDY Month/Day/Year mm/dd/yy / - . , & 8 01/15/96
*DMY Day/Month/Year dd/mm/yy / - . , & 8 15/01/96
*YMD Year/Month/Day yy/mm/dd / - . , & 8 96/01/15
*JUL Julian yy/ddd / - . , & 6 96/015
4-Digit Year Formats
*ISO International Standards Organization yyyy-mm-dd - 10 1996-01-15
*USA IBM® USA Standard mm/dd/yyyy / 10 01/15/1996
*EUR IBM European Standard . 10 15.01.1996
*JIS Japanese Industrial Standard Christian Era yyyy-mm-dd - 10 1996-01-15

Table 2 lists the *LOVAL, *HIVAL, and default values for all the RPG-defined date formats.

Table 2. Date Values
Format name Description *LOVAL *HIVAL Default Value
2-Digit Year Formats
*MDY Month/Day/Year 01/01/40 12/31/39 01/01/40
*DMY Day/Month/Year 01/01/40 31/12/39 01/01/40
*YMD Year/Month/Day 40/01/01 39/12/31 40/01/01
*JUL Julian 40/001 39/365 40/001
4-Digit Year Formats
*ISO International Standards Organization 0001-01-01 9999-12-31 0001-01-01
*USA IBM USA Standard 01/01/0001 12/31/9999 01/01/0001
*EUR IBM European Standard 01.01.0001 31.12.9999 01.01.0001
*JIS Japanese Industrial Standard Christian Era 0001-01-01 9999-12-31 0001-01-01

Several formats are also supported for fields used by the MOVE, MOVEL, and TEST operations only. This support is provided for compatibility with externally defined values that are already in a 3-digit year format and the 4-digit year *LONGJUL format. It also applies to the 2-digit year formats when *JOBRUN is specified.

*JOBRUN should be used when the field which it is describing is known to have the attributes from the job. For instance, a 12-digit numeric result of a TIME operation will be in the job date format.

Table 3 lists the valid externally defined date formats that can be used in Factor 1 of a MOVE, MOVEL, and TEST operation.

Table 3. Externally defined date formats and separators
Format Name Description Format (Default Separator) Valid Separators Length Example
2-Digit Year Formats
*JOBRUN1 Determined at runtime from the DATFMT, or DATSEP job values.
3-Digit Year Formats2
*CYMD Century Year/Month/Day cyy/mm/dd / - . , & 9 101/04/25
*CMDY Century Month/Day/Year cmm/dd/yy / - . , & 9 104/25/01
*CDMY Century Day/Month/Year cdd/mm/yy / - . , & 9 125/04/01
4-Digit Year Formats
*LONGJUL Long Julian yyyy/ddd / - . , & 8 2001/115
  1. *JOBRUN is valid only for character or numeric dates with a 2-digit year since the run-time job attribute for DATFMT can only be *MDY, *YMD, *DMY or *JUL.
  2. Valid values for the century character 'c' are:
        'c'           Years
         0          1900-1999
         1          2000-2099
         .              .
         .              .
         .              .
         9          2800-2899