CONVERT_DATE_TO_INTERNAL¶
Convert a user-formatted date string to ABAP's internal format (YYYYMMDD).
Purpose¶
The reverse of CONVERT_DATE_TO_EXTERNAL. Takes whatever date string the user typed or a source system produced — formatted according to the user's locale settings — and returns a clean TYPE D value. Use this when reading date input from free-text screen fields, flat files, or external systems that return locale-formatted date strings.
Signature¶
| Parameter | Direction | Type | Notes |
|---|---|---|---|
DATE_EXTERNAL |
IMPORTING | — | User-formatted date string to convert. |
ACCEPT_INITIAL_DATE |
IMPORTING | TADIR-EDTFLAG |
Optional. Pass 'X' to allow initial/empty dates without raising an exception. |
DATE_INTERNAL |
EXPORTING | — | Internal date (TYPE D, YYYYMMDD). |
Exceptions¶
| Exception | When raised |
|---|---|
DATE_EXTERNAL_IS_INVALID |
The value passed cannot be parsed as a valid date. |
Example¶
DATA: lv_date_int TYPE d.
" Value typed by the user in a free-text input field
" e.g. '15.01.2024' for a German-locale user
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = p_date
IMPORTING
date_internal = lv_date_int
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE |Invalid date: { p_date }| TYPE 'E'.
RETURN.
ENDIF.
" lv_date_int is now safe to use in SELECT WHERE conditions
SELECT * FROM vbak INTO TABLE @DATA(lt_orders)
WHERE audat = @lv_date_int.
" Allow initial/blank date without raising exception
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lv_input
accept_initial_date = 'X'
IMPORTING
date_internal = lv_date_int
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
Common pitfalls¶
Exception is raised on invalid input — handle it
Unlike some older date FMs, this one raises DATE_EXTERNAL_IS_INVALID on a bad value rather than returning silently. Always include the exception handler.
Initial dates
By default, an empty or initial date string raises DATE_EXTERNAL_IS_INVALID. Pass ACCEPT_INITIAL_DATE = 'X' if blank input is a valid scenario in your program.
- The FM interprets
DATE_EXTERNALaccording toSY-UNAME's date format setting. A hardcoded'DD.MM.YYYY'string will fail for a user whose locale expects'MM/DD/YYYY'. - For selection screen input, prefer
PARAMETERS p_date TYPE d— ABAP handles locale conversion automatically forTYPE Dparameters. - Do not pass a raw
SY-DATUMvalue — it is already in internal format and will be misinterpreted as a locale-formatted string.
See also¶
CONVERT_DATE_TO_EXTERNAL— reverse conversion: internal TYPE D → display string