Skip to Content
Technical Articles
Author's profile photo Wolfram Knan

ABAP: Converting a date datatype and vice versa

As a programmer formatting a date in ABAP can be very useful to be able to use it in different programmes, tables, function module calls and classes. It is always a lot of work to research possibilities to convert from one date type to another if there are existing many different types. For this reason I decided to write the following collection of techniques to handle data types:

 

1. The 1st example shows how to convert an internal date (i.e. sy-datum) to an external variable  i.e. a char:

  • INPUT:     21.06.2018 ( as a date datatype)
  • OUTPUT: ‘21.06.2018’ (as character)
DATA date_ext TYPE char10.

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal            = sy-datum
IMPORTING
date_external            = date_ext
EXCEPTIONS
date_internal_is_invalid = 1
OTHERS                   = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

 

2. A more short possibility as 1.:

date_ext = |{ sy-datum DATE = ENVIRONMENT }|.

 

 

3. The 2nd example shows how to convert an external date format (like a character) to an internal date format:

  • INPUT: ‘21.06.2018’ (as character)
  • OUTPUT: 21062018 (as a date datatype)
DATA date_int TYPE d.

CALL FUNCTION 'DATE_CONV_EXT_TO_INT'
  EXPORTING
    i_date_ext = '21.06.2018'
  IMPORTING
    e_date_int = date_int
  EXCEPTIONS
    error      = 1
    OTHERS     = 2.

IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

 

4. The 3rd example shows how you can convert a SAP date datatype to a character on your own without the help of an function module:

  • INPUT: 21.06.2018 (as sy-datum, internal as: 20180621)
  • OUTPUT: ‘21062018’ (as character)
DATA: date(10),
      new_date TYPE char10,
      day(2),
      month(2),
      year(4).

WRITE sy-datum TO date.

day(2) = date(2).
month(2) = date+3(2).
year(4) = date+6(4).
new_date = |{ day }| && |{ month }| && |{ year }|.

 

I hope I could help you and converting between different types will perhaps take a little bit less time in the future.

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.