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 month

ultimo = ultimo -
1. " = last day of this month

Here, the last day of the previous month is assigned to the date field
**ultimo**.

·
**ultimo** is filled
with the present date.

· Using an offset specification, the day is changed to the first day of the current month.

·
1 is
subtracted from **ultimo**. In this
way, the content of **ultimo** is changed
to the last day of the previous month. Before performing the subtraction, the
system converts **ultimo** to the
number of days since 01.01.0001 and converts the result back to a
date.

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.

·
First, the
difference between the time fields is calculated. This is -64800, since
**t1** is converted
internally to 72000 and **t2** to
7200.

·
Then, with
the operation **MOD**, this
negative difference is converted to the total number of seconds. A positive
difference would be unaffected by this calculation.

· Third, the number of hours is calculated by dividing the number of seconds by 3600.

The last three lines can be replaced by the following line

HOURS = ( ( t2 - t1) MOD 86400) / 3600.

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 INVERTED-DATE d1 INTO 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.

Output:

19955011

80049488

19955011