AS ABAP Release 758, ©Copyright 2024 SAP SE. All rights reserved.
ABAP - Keyword Documentation → ABAP - Programming Language → Processing Internal Data → Date and Time Processing → Date Fields and Time Fields → Access to Date Fields and Time Fields →Numeric Access to Date Fields and Time Fields
Numeric access to date fields and time fields exploits the fact that the conversion of the types d and t to numeric values produces an integer number of days or seconds. This applies particularly when using date fields and time fields in numeric calculations, where they are converted to the corresponding calculation type. This enables differences to be calculated or values to be added or subtracted from date fields or time fields. All other arithmetic operations are generally not useful here. To avoid unexpected results from such calculations, the validity of the content of the date or time fields must be ensured.
Example
The following calculations provide the current day of the year as well as the hour, minutes, and seconds of the current time. The date and time are provided by a time stamp.
DATA: date TYPE d,
time TYPE t,
day TYPE i,
hour TYPE i,
minute TYPE i,
second TYPE i,
first_day TYPE d.
FINAL(tzone) = cl_demo_date_time=>get_user_time_zone( ).
CONVERT UTCLONG utclong_current( )
INTO DATE date
TIME time
TIME ZONE tzone.
first_day = date(4) && '0101'.
day = date - first_day + 1.
second = time.
hour = second DIV 3600.
second = second - hour * 3600.
minute = second DIV 60.
second = second - minute * 60.
Example
The following calculation provides the day of the week for a date field date containing any valid date. 1 means Monday, 2 means Tuesday, and so on.
FINAL(date) = cl_demo_date_time=>get_user_date( ).
FINAL(day) = ( 5 + date MOD 7 ) MOD 7 + 1.
cl_demo_output=>display( day ).
Example
The following calculations provides the last day of the previous month.
DATA(date) = cl_demo_date_time=>get_user_date( ).
date+6(2) = '01'.
date = date - 1.