You are on page 1of 6

http://wiki.sdn.sap.

com/wiki/display/Snippets/ABAP++Function+module+for+converting+any+External+date+format+to+Internal+for mat

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 EXCEPTIONS

= ex_output

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.

DATA: g_date TYPE c LENGTH 10. CALL FUNCTION 'SLS_MISC_CONVERT_TO_DATE' EXPORTING p_date * P_DATE_FORMAT IMPORTING p_date_string * EXCEPTIONS * ERROR_SELECTING_USER_DEFAULTS * OTHERS . IF sy-subrc <> 0. * Implement suitable error handling here =2 =1 = g_date = sy-datum =

ELSE. WRITE: g_date. ENDIF.

User format is defaulted in (Menu option:System-->User Profile-->Own data) or can also be

found in USR01 table. Check DATFM field passing BNAME. Use domain value range of DATFM to

indentify what format is set for the user.

You might also like