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: 

Function Module for Date Conversion - 20060131 -> January 31, 2006

Former Member
0 Kudos

I can write some quick code and use the T015M table, but I figure there must be an existing function module. I want to convert a system date to a character string. Ideally, I would like it based on a country field.

20060131 -> January 31, 2006

20060131 -> 31 January 2006

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Norman,

There is a function module for month names :

MONTH_NAMES_GET

Hope this may help you.

Lanka

11 REPLIES 11

Former Member
0 Kudos

Hi Norman,

There is a function module for month names :

MONTH_NAMES_GET

Hope this may help you.

Lanka

0 Kudos

Using a combination of the function module that Lanka has suggested and some coding, you can achieve your result.

I don't have a system in front of me right now, so please bare with the code.

Call the function module MONTH_NAMES_GET to get the name of the month.

Here the field <b>the_month_name</b> is the name of the month coming back from MONTH_NAMES_GET.

data: date_string type string.
data: day_comma type string.
data: the_month(2) type c.
data: it247 type table of t247 with header line.

the_month = sy-datum+4(2).

CALL FUNCTION 'MONTH_NAMES_GET'
   exporting
       LANGUAGE = SY-LANGU
   TABLES
       month_names = it247
   EXCEPTIONS
       MONTH_NAMES_NOT_FOUND = 1
       OTHERS = 2.

read table it247 with key mnr = the_month.
the_month_name = it247-ltx.

* Here we put a comma after the day
<b>concatenate sy-datum+6(2) ',' into day_comma.

* here we are building the date string
concatenate the_month_name  day_comma sy-datum+0(4)
             into date_string separated by space.</b>

Write:/ date_string.




Regards,

Rich Heilman

Former Member
0 Kudos

u can try like this

&----


*& Report YCHATEST *

*& *

&----


*& *

*& *

&----


REPORT YCHATEST .

tables : t247.

data:it_t247 type standard table of t247 with header line.

data:v_year(4),

v_month(2),

v_day(2),

v_mon_desc(15),

v_date(35).

v_year = sy-datum+0(4).

v_month = sy-datum+4(2).

v_day = sy-datum+6(2).

CALL FUNCTION 'MONTH_NAMES_GET'

EXPORTING

LANGUAGE = SY-LANGU

  • IMPORTING

  • RETURN_CODE =

TABLES

month_names = it_t247

EXCEPTIONS

MONTH_NAMES_NOT_FOUND = 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.

read table it_t247 with key mnr = v_month.

v_mon_desc = it_t247-ltx.

concatenate v_day v_mon_desc v_year into v_date SEPARATED BY space.

write : v_date.

Former Member
0 Kudos

Hi Norman,

1. Here, we require TWO Things.

2. First : Getting the DATE FORMAT

Second : Getting the month names.

3. For both these,

we have 2 different FMs

4. I have developed one INDEPENDENT FORM/PERFORM,

in which we pass DATE,

and it returns the STRING January 31, 2006

(Based on SETTINGS)

5. use this code (just copy paste in new program)

6.

REPORT abc.

*----


DATA : datestr(25) TYPE c.

PARAMETER : d TYPE sy-datum DEFAULT sy-datum.

PERFORM getmonth USING d datestr.

WRITE 😕 datestr.

*----


  • FORM

*----


FORM getmonth USING d datestr.

DATA : month_names LIKE t247 OCCURS 0 WITH HEADER LINE.

DATA : m(2) TYPE c.

DATA : dformat(10) TYPE c.

DATA : mname(20) TYPE c.

m = d+4(2).

CALL FUNCTION 'MONTH_NAMES_GET'

  • EXPORTING

  • LANGUAGE = SY-LANGU

  • IMPORTING

  • RETURN_CODE =

TABLES

month_names = month_names

EXCEPTIONS

month_names_not_found = 1

OTHERS = 2

.

READ TABLE month_names INDEX m.

IF sy-subrc = 0.

mname = month_names-ltx.

ENDIF.

CALL FUNCTION 'SLS_MISC_GET_USER_DATE_FORMAT'

IMPORTING

p_date_format = dformat

EXCEPTIONS

error_reading_user_master = 1

error_reading_date_format_text = 2

OTHERS = 3.

datestr = dformat.

REPLACE 'DD' IN datestr WITH d+6(2).

REPLACE 'MM' IN datestr WITH mname.

REPLACE 'YYYY' IN datestr WITH d(4).

REPLACE ALL OCCURRENCES OF '.' IN datestr WITH ','.

ENDFORM. "getmonth

regards,

amit m.

Former Member
0 Kudos

Norman

A simpler solution is to look at the edit masks for date fields, thus:

WRITE SY-DATLO USING EDIT MASK '==LDATE'.

To find the edit mask to use, test the function associated with the mask. The functions have the name format 'CONVERSION_EXIT_ldate_OUTPUT'. (Use the SET command to change country / language.)

MattG.

0 Kudos

Thanks everyone. Great suggestions. I really thought there would be an existing function module to do this task.

I ended up getting the country date format from T005-DATFM, month name from table T015M, and then formatting as needed.

Thanks again!

0 Kudos

Hi

Try to see the program use table T247 instead of T015M, for example the fm CONVERSION_EXIT_IDATE_OUTPUT:

it returns the date in this format:

20063101 -> 31JAN2006

Max

0 Kudos

I liked the idea of the conversion exit and EDIT_MASK, but they want the full month name (January instead of JAN). I could not find an EDIT MASK with the full name. I could replace the short month in table T247 with the long name, but the code seems to be working.

FORM format_date USING P_LV_LAND1 type land1.

data: lv_month_txt type monam,

lv_month type monum,

lv_day(2) type c,

lv_year(4) type n,

lv_datfm type datfm.

clear: gv_date_text,

lv_month_txt.

lv_month = sy-datum+4(2).

lv_day = sy-datum+6(2).

lv_year = sy-datum+0(4).

shift lv_day left deleting leading '0'.

  • get month text

select single monam into lv_month_txt

from t015m

where spras = sy-langu

and monum = lv_month.

check sy-subrc = 0.

  • get formatting key for ship-to country

select single datfm into lv_datfm

from t005

where land1 = p_lv_land1.

check sy-subrc = 0.

  • format based on country format key

case lv_datfm.

  • DD.MM.YYYY

when '1'.

concatenate lv_day lv_month_txt lv_year

into gv_date_text separated by space.

  • MM/DD/YYYY or MM-DD-YYYY

when '2' or '3'.

concatenate lv_day gc_comma into gv_date_text.

condense gv_date_text no-gaps.

concatenate lv_month_txt gv_date_text lv_year

into gv_date_text separated by space.

  • YYYY.MM.DD

when '4'.

concatenate lv_year lv_month_txt lv_day

into gv_date_text separated by space.

  • default Month DD, YYYY

when others.

concatenate lv_day gc_comma into gv_date_text.

condense gv_date_text no-gaps.

concatenate lv_month_txt gv_date_text lv_year

into gv_date_text separated by space.

endcase.

condense gv_date_text.

ENDFORM. " format_date

0 Kudos

Your code seems to be working good.

Regards,

Rich Heilman

0 Kudos

Norman

Strange I've used edit mask LDATE, function CONVERSION_EXIT_LDATE_OUTPUT, and get the full month name.

Anyway, here is a little program to see the outputs for different countries. It does need updating for edit mask MODAT, and checking the return code after select and function calls:

REPORT ZZMATTG004 .

DATA: D8(20),
      WA_MASK LIKE DD01D-CONVEXIT,
      MONTH_NAMES LIKE T247 OCCURS 12 WITH HEADER LINE.
TABLES: T005.                          "Countries
PARAMETERS: DATE LIKE SY-DATUM DEFAULT SY-DATLO.
SELECT-OPTIONS S_LAND FOR T005-LAND1 NO INTERVALS.
PARAMETERS: P_LANGU AS CHECKBOX DEFAULT 'X'.

D8  =  DATE.
DESCRIBE FIELD DATE EDIT MASK WA_MASK.
WRITE: 'MASK IS :- ''', WA_MASK NO-GAP, '''' NO-GAP.
SKIP.
WRITE:
       / DATE DD/MM/YYYY, 22 'DD/MM/YYYY',
       / DATE MM/DD/YYYY, 22 'MM/DD/YYYY',
       / DATE DD/MM/YY  , 22 'DD/MM/YY',
       / DATE MM/DD/YY  , 22 'MM/DD/YY',
       / DATE DDMMYY    , 22 'DDMMYY',
       / DATE MMDDYY    , 22 'MMDDYY',
       / DATE YYMMDD    , 22 'YYMMDD'.
SKIP.
WRITE:
       / D8   USING EDIT MASK '==LDATE', 'LDATE',
       / D8   USING EDIT MASK '==SDATE', 'SDATE',
       / D8   USING EDIT MASK '==IDATE', 'IDATE',
       / D8   USING EDIT MASK '==D3DAT', 'D3DAT',
       / D8   USING EDIT MASK '==PDATE', 'PDATE',
       / D8   USING EDIT MASK '==INVD1', 'INVD1',
       / D8   USING EDIT MASK '==INVDT', 'INVDT'.

LOOP AT S_LAND.
* BREAK-POINT.
  SET COUNTRY S_LAND-LOW.
  SELECT SINGLE SPRAS INTO T005-SPRAS FROM T005
         WHERE  LAND1  =  S_LAND-LOW.
  IF  P_LANGU  =  'X'.
    SET LANGUAGE T005-SPRAS.
  ENDIF.
  REFRESH MONTH_NAMES.
  CALL FUNCTION 'MONTH_NAMES_GET'
       EXPORTING
            LANGUAGE              = T005-SPRAS
*      IMPORTING
*           RETURN_CODE           =
       TABLES
            MONTH_NAMES           = MONTH_NAMES
       EXCEPTIONS
            MONTH_NAMES_NOT_FOUND = 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.
* READ TABLE MONTH_NAMES INDEX DATE+4(2).
  READ TABLE MONTH_NAMES WITH KEY MNR  =  DATE+4(2).

  SKIP.
  WRITE: / 'COUNTRY SET TO', S_LAND-LOW COLOR 2,
         / 'Month name is', MONTH_NAMES-KTX, MONTH_NAMES-LTX,
           'Language (T005) is', T005-SPRAS, '(SYST)', SY-LANGU.
  WRITE:
         / DATE DD/MM/YYYY, 22 'DD/MM/YYYY',
         / DATE MM/DD/YYYY, 22 'MM/DD/YYYY',
         / DATE DD/MM/YY  , 22 'DD/MM/YY',
         / DATE MM/DD/YY  , 22 'MM/DD/YY',
         / DATE DDMMYY    , 22 'DDMMYY',
         / DATE MMDDYY    , 22 'MMDDYY',
         / DATE YYMMDD    , 22 'YYMMDD'.
  SKIP.
  WRITE:
         / D8   USING EDIT MASK '==LDATE', 'LDATE',
         / D8   USING EDIT MASK '==SDATE', 'SDATE',
         / D8   USING EDIT MASK '==IDATE', 'IDATE',
         / D8   USING EDIT MASK '==D3DAT', 'D3DAT',
         / D8   USING EDIT MASK '==PDATE', 'PDATE',
         / D8   USING EDIT MASK '==INVD1', 'INVD1',
         / D8   USING EDIT MASK '==INVDT', 'INVDT'.
ENDLOOP.
SET COUNTRY SPACE.
* SET LANGUAGE T005-SPRAS.
  REFRESH MONTH_NAMES.
  CALL FUNCTION 'MONTH_NAMES_GET'
       EXPORTING
            LANGUAGE              = SY-LANGU
*      IMPORTING
*           RETURN_CODE           =
       TABLES
            MONTH_NAMES           = MONTH_NAMES
       EXCEPTIONS
            MONTH_NAMES_NOT_FOUND = 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.
* READ TABLE MONTH_NAMES INDEX DATE+4(2).
  READ TABLE MONTH_NAMES WITH KEY MNR  =  DATE+4(2).

  SKIP.
  WRITE: / 'COUNTRY SET TO', 'SPACE'    COLOR 2,
         / 'Month name is', MONTH_NAMES-KTX, MONTH_NAMES-LTX.
  WRITE:
         / DATE DD/MM/YYYY, 22 'DD/MM/YYYY',
         / DATE MM/DD/YYYY, 22 'MM/DD/YYYY',
         / DATE DD/MM/YY  , 22 'DD/MM/YY',
         / DATE MM/DD/YY  , 22 'MM/DD/YY',
         / DATE DDMMYY    , 22 'DDMMYY',
         / DATE MMDDYY    , 22 'MMDDYY',
         / DATE YYMMDD    , 22 'YYMMDD'.
  SKIP.
  WRITE:
         / D8   USING EDIT MASK '==LDATE', 'LDATE',
         / D8   USING EDIT MASK '==SDATE', 'SDATE',
         / D8   USING EDIT MASK '==IDATE', 'IDATE',
         / D8   USING EDIT MASK '==D3DAT', 'D3DAT',
         / D8   USING EDIT MASK '==PDATE', 'PDATE',
         / D8   USING EDIT MASK '==INVD1', 'INVD1',
         / D8   USING EDIT MASK '==INVDT', 'INVDT'.

MattG.

0 Kudos

Thanks Matt. Unfortunately this task is over, the code is in production, and any changes would require several management authorizations.

When I looked at that function module, the text showed JAN as an example. I didn't have much time to experiment. I was only allocated 30 minutes to complete the task (code and test) of changing the date from MM/DD/YY to a full month name in the format of the destination country. Didn't have time to try out many new things.

I saved your code and I'm sure it will come in handy in the future.

Thanks again.