Saturday, 3 February 2007

ABAP Date Time Variables and Addressing Subfields

Date and Time Data types.

Type d is the data type used for dates and type t for time. Both consist of string representations of dates (in a YYYMMDD format) and times (HHMMSS) with which calculations can be performed. Adding an integer value to a type d variable affects the day part of the date while adding an integer to a time value affects seconds. Assigning a date or time value to an integer yields the number of days since 01.01.0001 or the number of seconds since 00:00:00. The system date is provided by the sy-datum system field while the system time by sy-uzeit.

Addressing subfields

Extracting date and time fields from date and time variables can be achieved by using the SAP sub-field syntax which -- given a filed named f -- goes like this :

subfield = f[+offset][(length)]

if you specify an offset without length then the entire string starting from offset is addressed. if no offset is specified then 1 is implied. ABAP strings always start at index 1.

Examples

Keeping in mind that the internal format of a date field is YYYYMMDD we may declare some variables using the following syntax
DATA :
  my_birthday TYPE d VALUE '19681120',
  cur_year TYPE i VALUE sy-datum(4),    " get the first four characters of the sy-datum field
  cur_month TYPE i VALUE sy-datum+4(2), " get the two characters after position 4 i.e. 5 and 6
  cur_day TYPE i VALUE sy-datum+6(2).    " get the two characters after position 6 i.e. 7 and 8

No comments :