ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Date and Time Processing → Date Fields and Time Fields
Validity of Date Fields and Time Fields
Date fields and time fields of the types d
and t
can contain any alphanumeric characters.
- Valid values for the type
d
, however, are only those digits that are valid dates according to the calendar rules in the format "yyyymmdd": "yyyy" (year): 0001 to 9999, "mm" (month): 01 to 12, "dd" (day): 01 to 31.
- Valid values for the type
t
, however, are only those digits that are interpreted as times in the 24-hour clock format "hhmmss". "hh" (hours): 00 to 23, "mm" (minutes): 00 to 59, "ss" (seconds): 00 to 59.
The ABAP runtime environment checks the validity at the following points:
- Date fields and time fields with invalid values raise an exception when used as a source field of a lossless assignment. Conversely, source fields of other types may not produce any invalid values in date fields and time fields as target fields in a lossless assignment.
- Invalid values cannot be converted to time stamps
by using the statements CONVERT
INTO UTCLONG and
CONVERT INTO TIME STAMP
.
- In a regular assignment to numeric fields, date fields and target fields with invalid values produce the value 0, with the following exceptions:
- For a source field of the type
d
, the values from 10/5/1582 to 10/14/1582, missing due to the conversion from the Julian to Gregorian calendar, are handled either as invalid values or like the valid values from 10/15/1582 to 10/24/1582, depending on the operand position.
- For a source field of the type
t
that contains only digits, the numeric value is calculated using the formula hh*3600+mm*60+ss, even if the times are invalid.
- For regular assignments of numeric fields
to date fields and time fields, the numeric fields must be within the value range of the data type
i
and comply with the following special rules:
- When number values are assigned to date fields, and these fields cannot be produced by the reverse conversion, the date field is initialized
- Any number values can be assigned to time fields. When converted, the number value is divided by 86400 and only the integer remainder of the division is respected. This always produces a valid time in hours.
No checks are generally made on other assignments to date fields and time fields not mapped to assignments of numeric values, nor to other operand positions. As a consequence, invalid values are produced easily by date fields and time fields, and then processed further.
Other versions: 7.31 | 7.40 | 7.54
Initial Value of Date Fields
The initial value "00000000" of the data type d
and the date 01.01.0001 have a special part to play here.
- The initial value "00000000" of data type
d
is not a valid date. A conversion of the initial value "00000000" to a numeric data type produces, like all invalid values, the value 0. Conversely, the conversion of the number 0 to a date field of the typed
always produces the invalid date "00000000". Negative and oversized numbers also produce the invalid date "00000000".
- A conversion of a valid date to a field of the type
d
produces the number of days since 01.01.0001, which makes this date the zero point for date calculations. A conversion of a date field of the typed
with the value "00010101" to a numeric value produces the value 0, like the conversion of the initial value or any other invalid value, and the conversion cannot be reversed.
1 is the smallest number that is converted to a valid date, 02.01.0001, when assigning to a date field
of type d
. Assignments between valid date fields and numeric fields can be reversed only from this date.
In lossless assignments, invalid values in source fields raise exceptions instead of producing the value 0 or "00000000". Here, the value "00010101", which is really part of the value range, is regarded as invalid, while "00000000", not actually part of the value range, is regarded as valid. This means that the initial value "00000000" can be used by lossless assignments and all valid assignments between date fields and numeric fields can be reversed.
ABAP SQL is excluded from this rule. Here, the value "00010101" is also accepted as a valid date and treated like the initial value "00000000" in operand positions for which values must satisfy the rules for lossless assignments.
Note
The validity of the content of date fields and time fields must be verified before they are accessed.
Example
Identify an invalid date by a comparison with the value 0. For the comparison, the date field is converted
to an integer field of type i
, where an invalid date produces the value 0. The valid date "00010101", which would also produce the value 0, is handled separately beforehand.
DATA(date) = CONV d( '20160231' ).
date = COND #( WHEN date = '00010101' OR date <> 0 THEN date
ELSE THROW cx_sy_conversion_no_date( ) ).
Example
Identify an invalid time by a comparison with the value 0. For the comparison, the time field is converted
to an integer field of type i
, where an invalid time produces the value 0. The valid time "000000", which would also produce the value 0, is handled separately beforehand.
DATA(time) = CONV t( 'XXXXXX' ).
time = COND #( WHEN time = '000000' OR time <> 0 THEN time
ELSE THROW cx_sy_conversion_no_time( ) ).