Arithmetic operations that include a date field are restricted to:
Date field operands are compatible if they have the same date format except for the year part, which can be windowed or expanded.
The following operations are not allowed:
The sections below describe the result of using date fields in the supported addition and subtraction operations.
For more information about using date fields in arithmetic operations, see:
Usage notes
The following table shows the result of using a date field with a compatible operand in an addition.
| Nondate second operand | Date field second operand | |
|---|---|---|
| Nondate first operand | Nondate | Date field |
| Date field first operand | Date field | Not allowed |
For details on how a result is stored in a receiving field, see Storing arithmetic results that involve date fields.
The following table shows the result of using a date field with a compatible operand in the subtraction:
first operand - second operand
In a SUBTRACT statement, these operands appear in the reverse order:
SUBTRACT second operand FROM first operand
The following statements perform arithmetic, then store the result, or sending field, into one or more receiving fields:
In a MULTIPLY statement, only GIVING identifiers can be date fields. In a DIVIDE statement, only GIVING identifiers or the REMAINDER identifier can be date fields.
Any windowed date fields that are operands of the arithmetic expression or statement are treated as if they were expanded before use, as described under Semantics of windowed date fields.
If the sending field is a date field, then the receiving field must be a compatible date field. That is, both fields must have the same date format, except for the year part, which can be windowed or expanded.
If the ON SIZE ERROR clause is not specified on the statement, the store operation follows the existing COBOL rules for the statement and proceeds as if the receiving and sending fields (after any automatic expansion of windowed date field operands or result) were both nondates.
Table 3 shows how these statements store the value of a sending field in a receiving field, where either field can be a date field. The section uses the following terms to describe how the store is performed:
Size error processing: For both kinds of sending field, if the assumed or actual year part of the sending field falls within the century window, the sending field is stored in the receiving field after removing the century component of the year part. That is, the low-order or rightmost two digits of the expanded year part are retained and the high-order or leftmost two digits are discarded.
If the year part does not fall within the century window, then the receiving field is unmodified, and the size error imperative statement is executed when any remaining arithmetic operations are complete.
For example:
77 DUE-DATE PICTURE 9(5) DATE FORMAT YYXXX.
77 IN-DATE PICTURE 9(8) DATE FORMAT YYYYXXX VALUE 1995001.
...
COMPUTE DUE-DATE = IN-DATE + 10000
ON SIZE ERROR imperative-statement
END-COMPUTE
The sending field is an expanded date field representing January 1, 2005. Assuming that 2005 falls within the century window, the value stored in DUE-DATE is 05001; that is, the sending value of 2005001 without the century component 20.
Size error processing and trigger values: If the DATEPROC(TRIG) compiler option is in effect and the sending field contains a trigger value (either zero or all nines), the size error imperative statement is executed and the result is not stored in the receiving field.
A nondate is considered to have a trigger value of all nines if it has a nine in every digit position of its assumed date format. Thus for a receiving date format of YYXXX, the nondate value 99,999 is a trigger but the values 9,999 and 999,999 are not, although the larger value of 999,999 will cause a size error anyway.
| Nondate sending field | Date field sending field | |
|---|---|---|
| Nondate receiving field | Nonwindowed | Not allowed |
| Windowed date field receiving field | Windowed | Windowed |
| Expanded date field receiving field | Nonwindowed | Nonwindowed |