Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 11577

Re: How to convert date into a specific format irrespective of the format given as input?

$
0
0

You can apply this idea.

 

  lv_datfm = cl_abap_datfm=>get_datfm( ).


 TRY.
      CALL METHOD cl_abap_datfm=>get_date_format_des
        EXPORTING
          im_datfm      = lv_datfm
          im_langu      = sy-langu
          im_plain      = abap_false
          im_long       = abap_false
        IMPORTING
          ex_dateformat = lv_format.
    CATCH cx_abap_datfm_format_unknown .
      RETURN.
  ENDTRY.


  TRY.
      CALL METHOD cl_abap_datfm=>get_delimiter
        EXPORTING
          im_datfm     = lv_datfm
        IMPORTING
          ex_delimiter = lv_delimeter.
    CATCH cx_abap_datfm_format_unknown .
      RETURN.
  ENDTRY.


  IF lv_format IS NOT INITIAL.
    SPLIT lv_format AT lv_delimeter INTO TABLE lt_form.
    LOOP AT lt_form INTO ls_form.
      IF ls_form(1) = 'Y'.
        CONCATENATE mv_dmy 'J' INTO mv_dmy.
      ELSEIF ls_form(1) = 'M'.
        CONCATENATE mv_dmy 'M' INTO mv_dmy.
      ELSEIF ls_form(1) = 'D'.
        CONCATENATE mv_dmy 'T' INTO mv_dmy.
      ENDIF.
    ENDLOOP.
  ENDIF.

 

 

Now you know the year month and date position, based on this you can map and convert the incoming date. This can be done only if the incoming date format is fixed, ie if you know the positions of date/month/year of incoming value. To take data from excel we use to set the sap user date format in excel in OLE and then while uploading the data we use to get the internal date format as below:

 

  CALL FUNCTION 'KCD_EXCEL_DATE_CONVERT'
    EXPORTING
      excel_date  = iv_value               "Incoming date         
      date_format = me->mv_dmy
    IMPORTING
      sap_date    = lv_date.               "SAP format


  CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
    EXPORTING
      date                      = lv_date
    EXCEPTIONS
      plausibility_check_failed = 1.
  IF sy-subrc <> 0.
    CLEAR rv_result.
  ELSE.
    rv_result = lv_date.
  ENDIF.


Viewing all articles
Browse latest Browse all 11577

Trending Articles