How to format date as per the login user profile.
Many times we need to format the date in smartforms or scrips as per the login user details. some do it some face some problems.
so, here is a simple function module I implemented and i want to share. The main point is all user settings like this are stored in SAP
DB table USR01; field datfm which contains a 1 char value which signifies the following:
*1 DD.MM.YYYY
*2 MM/DD/YYYY
*3 MM-DD-YYYY
*4 YYYY.MM.DD
*5 YYYY/MM/DD
*6 YYYY-MM-DD
*7 GYY.MM.DD Japanese Date
*8 GYY/MM/DD Japanese Date
*9 GYY-MM-DD Japanese Date
*A YYYY/MM/DD Islamic Date 1
*B YYYY/MM/DD Islamic Date 2
*C YYYY/MM/DD Iranian Date
Here is what i did:
1. The user may enter date in various formats as allowed so get it in internal format YYYYMMDD by passing the date
to be manipulated to FM CONVERT_DATE_TO_INTERNAL
get the export parameter let’s call it date_internal_fmt.
2. Call the z-Function module pass it the username and the date in a variable of type sydatum (value from date_internal_fmt).
3. Receive the export parameter in a field of character type length 10 with full formatting.
Source code:
FUNCTION z_man_n_date_in_user_profile.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” REFERENCE(IM_USERNAME) TYPE SY-UNAME
*” REFERENCE(IM_DAT) TYPE DATS
*” EXPORTING
*” REFERENCE(EX_DATE) TYPE CHAR10
*” EXCEPTIONS
*” ERROR_IN_READING_USR01
*”———————————————————————-
*1 DD.MM.YYYY
*2 MM/DD/YYYY
*3 MM-DD-YYYY
*4 YYYY.MM.DD
*5 YYYY/MM/DD
*6 YYYY-MM-DD
*7 GYY.MM.DD Japanese Date
*8 GYY/MM/DD Japanese Date
*9 GYY-MM-DD Japanese Date
*A YYYY/MM/DD Islamic Date 1
*B YYYY/MM/DD Islamic Date 2
*C YYYY/MM/DD Iranian Date
DATA: l_datfm TYPE xudatfm,
l_yearl TYPE c LENGTH 4,
l_years TYPE c LENGTH 2,
l_month TYPE c LENGTH 2,
l_dat TYPE c LENGTH 2,
im_date TYPE c LENGTH 8.
im_date = im_dat.
* FIRST GET THE MONTH DATE AND YEAR COMPONENTS FROM THE DAT
MOVE im_date+0(4) TO l_yearl.
MOVE l_yearl+2(2) TO l_years.
MOVE im_date+4(2) TO l_month.
MOVE im_date+6(2) TO l_dat.
* read user profile from table USR01
SELECT SINGLE datfm FROM usr01 INTO l_datfm WHERE bname = im_username.
IF sy-subrc = 0.
CASE l_datfm.
WHEN ‘1’.
CONCATENATE l_dat l_month l_yearl INTO ex_date SEPARATED BY ‘.’.
WHEN ‘2’.
CONCATENATE l_month l_dat l_yearl INTO ex_date SEPARATED BY ‘/’.
WHEN ‘3’.
CONCATENATE l_month l_dat l_yearl INTO ex_date SEPARATED BY ‘-‘.
WHEN ‘4’.
CONCATENATE l_yearl l_month l_dat INTO ex_date SEPARATED BY ‘.’.
WHEN ‘5’ OR ‘A’ OR ‘B’ OR ‘C’.
CONCATENATE l_yearl l_month l_dat INTO ex_date SEPARATED BY ‘/’.
WHEN ‘6’.
CONCATENATE l_yearl l_month l_dat INTO ex_date SEPARATED BY ‘-‘.
WHEN OTHERS.
RAISE error_in_reading_usr01.
ENDCASE.
ENDIF.
ENDFUNCTION.
Nice work. But you are excluding the following date formats:
*7 GYY.MM.DD Japanese Date
*8 GYY/MM/DD Japanese Date
*9 GYY-MM-DD Japanese Date
*A YYYY/MM/DD Islamic Date 1
*B YYYY/MM/DD Islamic Date 2
*C YYYY/MM/DD Iranian Date
Any idea how to handle them?