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 → 
Mail Feedback

Character-Like Access to Date Fields and Time Fields

Character-like access to character-like content of date fields and time fields is evaluated in a character-like way. The character-like nature of date fields and time fields can be exploited, for example, to access detailed information. To avoid unexpected results from this type of access, the validity of the content of the date or time fields must be ensured. Most statements and functions used in character string processing are not suitable for processing date fields and time fields because they generally produce invalid content.

Example

The following example demonstrates how substring functions can be used to extract the components year, month, day, hour, minute, and second from date fields and time fields.

DATA: date TYPE d,
      time TYPE t.

DATA: year   TYPE i,
      month  TYPE i,
      day    TYPE i,
      hour   TYPE i,
      minute TYPE i,
      second TYPE i.

date = cl_demo_date_time=>get_user_date( ).
time = cl_demo_date_time=>get_user_time( ).

year   = substring( val = date off = 0 len = 4 ).
month  = substring( val = date off = 4 len = 2 ).
day    = substring( val = date off = 6 len = 2 ).
hour   = substring( val = time off = 0 len = 2 ).
minute = substring( val = time off = 2 len = 2 ).
second = substring( val = time off = 4 len = 2 ).