How do I convert normal dates into SAP dates?
Submitted by Anonymous (not verified) on Thu, 06/09/2011 - 16:06
I'm transporting Accounts Payable documents and the dates from the source data sometimes come in the format MM/DD/YYYY. The SAP system errors out in background processing if the date isn't in the format YYYYMMDD. In fact, all the date fields for BDC background processing and IDOCs are expected in the form YYYYMMDD. How can I ensure that my dates are always formatted correctly?
Duration:
1 month
Comments
A simple form to convert MM/DD/YYYY to YYYYMMDD
I see this all the time. At first I wrote Access VBA functions to catch this sort of thing, but quickly tired of maintaining this in so many places. Then I wrote a short form that catches malformed dates and converts them to SAP's desired format. Here is the code that I used.
The form splits the date on the slash into three parts and then pads the month and day with zeros to make two digits. Then, with a simple concatenation, it combines the year, month and day into a single 8-character date suitable for SAP. Pass the source date into the form as the used parameter and make the target field in the document the changed parameter. It's that easy.
It is also easy to convert dates to SAP's YYYYMMDD format in VBA using this snippet. Either solution will work fine to prepare source data for SAP.
Convert any external date into YYYYMMDD
If your SAP implementation project is in an international environment it could happen that localized data - like the date format - varies.
In order to avoid code changes due to a date format change in the source I'd recommend to use the following standard SAP-function:
--------------------------------------------
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = gv_input_date
* ACCEPT_INITIAL_DATE =
IMPORTING
DATE_INTERNAL = gv_output_date
* EXCEPTIONS
* DATE_EXTERNAL_IS_INVALID = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
--------------------------------------------
The function will convert the input date into the internal SAP date format. The expected input format is taken from your user settings in SAP. Unfortunately this means that there's a direct correlation between user settings and source file.
Therefore in most cases Jimbo's solution above is the way to go because the code is based on the requirements set by the source file and avoids the user settings dependency.