Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

Hi,

Is there a smart way (Function Module/ Reusable code/ Std FM) to convert a date entered in any format (dd.mm.yyyy/ yyyymmdd/ ....) into a specific format only say dd.mm.yyyy?

OR

How to convert date to a specific format (say. dd.mm.yyyy) irrespective of the format in which it is entered (yyyymmdd/ mm.dd.yyyy/...)?

Thanks in advance,

prajakt

14 REPLIES 14

Former Member
0 Kudos

One way is to use BKK_CONVERT_FORMAT_DATE to convert Format Date from YYYYMMDD to DD.MM.YYYY

alex_campbell
Contributor
0 Kudos

In my experience there's no 100% perfect standard for entering dates. In the end, there's always some user who wants to enter MM.DD.YYYY and another who wants to enter DD.MM.YYYY. It would be a good idea to have the functional requirements documented around how to interpret dates.

If you can get the users to maintain their user settings properly, you can use FM CONVERT_DATE_TO_INTERNAL to convert dates from their configured format into internal format. Then from internal format you can use WRITE ... MM/DD/YYYY or some other logic to convert it to the desired format.

Former Member
0 Kudos

Well, it's simple but can you get the format it was introduced from?

or is that what you want?

By the way if there is 12122012, you can't infer if it is day or month.

Former Member
0 Kudos

Hi,

Sorry guys if my question was not to the point.

I need to convert date format YYYYMMDD in the date format which has been set in the user preference.

Reason is this date YYYYMMDD is coming from an interface and then displayed on screen. For ex: if the date is 20121016 and user preference of date display in SU01 is DD.MM.YYYY, it should take the 20121016 and convert it to 16.10.2012 which is seen as filled on screen.

If user preference is MM.DD.YYYY then it should be displayed as MM.DD.YYYY on the screen in that particular date field.

Can you kindly suggest?

Appreciate your replies on this!

Thanks in advance ,

Prajakt Dhumal

0 Kudos

Hi Prajakt,

The best way is to use offset.

Suppose if the date say sy-datum is in format yyyymmdd and you want to convert it to dd.mm.yyyy

try using this logic :

data:  date1(10) type c,
         day(2) type c,
         month(2) type c,
         year(4) type c.

year = sy-datum+0(4).
month = sy-datum+4(2).
day = sy-datum+6(2).

concatenate day month year into date1 separated by '.'.
condense date1.

Regards...

Sultana

0 Kudos

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 before hand.

To take data from excel we use to set the sap user date format in excel column in OLE while providing the upload template from the program itself 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.

Former Member
0 Kudos

Hi,

If you are using write statement ,then you can use its format.

DD/MM/YY   | MM/DD/YY

    | DD/MM/YYYY | MM/DD/YYYY

    | DDMMYY     | MMDDYY

    | YYMMDD ] ... .

Example:

Write date1 YYMMDD."date1 is the variable which has date value.

Regards,

Priyanka

Former Member
0 Kudos

Prajakt,

The function module CONVERT_DATE_TO_EXTERNAL will be used to convert the date from internal format to external format. YYYYMMDD is the internal SAP format of date, while the external format is based on the user settings. Some user settings may have MM/DD/YYYY or MM.DD.YYYY, which is seen on the screen. The above function module till check the user decimal notation settings and convert the date into the screen format.

Former Member
0 Kudos

CALL FUNCTION 'FORMAT_DATE_4_OUTPUT'

   EXPORTING

     DATIN         = DATE_TO_FORMAT

     FORMAT        = 'YYYYMMDD'

  IMPORTING

    DATEX         = output_string_Date.

Thats how you format a date.

Former Member
0 Kudos

Hi Prajakt,

The above mentioned Functional module will surely help you.

Please Try the same ,i have used it and got the result

Regards,

Praveen Srivastava

anupam_anand
Participant
0 Kudos

Hi Prajakt,

I am not sure about how are you trying to display this date on the screen.

Incase you are doing a write statement, it will automatically pick up the user format and display the date accordingly.

Same is the case with ALV too.

Also, you can write the value of this in another variable as:

lv_date = '20131108'.

WRITE: lv_date to lv_date1 MM/DD/YY.

OR

WRITE: LV_DATE TO LV_DATE1 MM/DD/YYYY

This way you can force your date formats too.

Do let me know if it helped.

Thanks,

Anupam

0 Kudos

Hello praveen Hello

Did you checked the time when Former Memberasked this question?? Its more then a year.. why are you re-opening this thread..?

0 Kudos

Oops...My bad...just noticed this in the contents and thought it was still open...Thanks for pointing this out

0 Kudos

Thanks for your understanding

Enjoy the weekend ...