
Date and time fields have character types, not numeric ones. However, you can still use date and time fields in numeric operations. To allow you to do so, the system performs automatic type conversions . You may also find it useful to use offset and length specifications when using date and time fields in calculations.
Example of a date calculation:
DATA: ultimo TYPE d.
ultimo = sy-datum. ultimo+6(2) = '01'. " = first day of this monthultimo = ultimo - 1. " = last day of previous month
Here, the last day of the previous month is assigned to the date field ultimo.
Example of a time calculation:
DATA: diff TYPE i, seconds TYPE i, hours TYPE i.
DATA: t1 TYPE t VALUE '200000', t2 TYPE t VALUE '020000'.
diff = t2 - t1.seconds = diff MOD 86400.hours = seconds / 3600.
The number of hours between 02:00:00 and 20:00:00 is calculated.
The last three lines can be replaced by the following line
HOURS = ( ( t2 - t1) MOD 86400) / 3600.
Inverted Dates
These statements are obsolete and are only available to ensure compatibility with Releases prior to 4.6 and 6.10. The statements may appear in older programs but should no longer be used.
In some cases (for example, when sorting dates in descending order), it is useful to convert a date from format d to an inverted date by using the keyword CONVERT.
CONVERT DATE d1 INTO INVERTED-DATE d2.
Afterwards, you can convert the inverted data back into a normal date using the statement
CONVERT DATE d1 INTO INVERTED-DATE d2.
These statements convert the field d1 from the formats DATE or INVERTED-DATE to the formats INVERTED-DATE or DATE and assign it to the field d2.
For the conversion, ABAP forms the nine's complement.
DATA: odate TYPE d VALUE '19955011', idate LIKE odate.
DATA field(8) TYPE c.
field = odate. WRITE / field.
CONVERT date odate INTO INVERTED-DATE idate.
field = idate. WRITE / field.
CONVERT INVERTED-DATE idate INTO DATE odate.
field = odate. WRITE / field.
The output looks like this:
19955011
80049488
19955011