Description: This is function module can be used for converting any External date format to Internal format.
The following SAP's external formats which can be found in the user settings are supported in this function module.
DD.MM.YYYY
MM/DD/YYYY
MM-DD-YYYY
YYYY.MM.DD
YYYY/MM/DD
YYYY-MM-DD
The function module needs to created with the parameters as mentioned in the code.
FUNCTION z_convert_to_internal_format.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" VALUE(IM_INPUT) TYPE CHAR10
*" EXPORTING
*" VALUE(EX_OUTPUT) TYPE SYDATUM
*" EXCEPTIONS
*" INVALID_DATE
*"----------------------------------------------------------------------
* Get the format.
* DD.MM.YYYY
IF im_input+2(1) = '.'.
CONCATENATE im_input+6(4) im_input+3(2) im_input(2) INTO ex_output.
ENDIF.
* MM/DD/YYYY
IF im_input+2(1) = '/'.
CONCATENATE im_input+6(4) im_input(2) im_input+3(2) INTO ex_output.
ENDIF.
* MM-DD-YYYY
IF im_input+2(1) = '-'.
CONCATENATE im_input+6(4) im_input(2) im_input+3(2) INTO ex_output.
ENDIF.
* YYYY.MM.DD
IF im_input+4(1) = '.'.
CONCATENATE im_input(4) im_input+5(2) im_input+8(2) INTO ex_output.
ENDIF.
* YYYY/MM/DD
IF im_input+4(1) = '/'.
CONCATENATE im_input(4) im_input+5(2) im_input+8(2) INTO ex_output.
ENDIF.
* YYYY-MM-DD
IF im_input+4(1) = '-'.
CONCATENATE im_input(4) im_input+5(2) im_input+8(2) INTO ex_output.
ENDIF.
* Check the valid date.
CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
EXPORTING
date = ex_output
EXCEPTIONS
plausibility_check_failed = 1
OTHERS = 2.
* Check if the output parameter is populated. otherwise raise an
* exception.
IF ex_output IS INITIAL OR sy-subrc <> 0.
RAISE invalid_date.
ENDIF.
ENDFUNCTION.