Application Development and Automation 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: 
Read only

convert date format

Former Member
0 Likes
2,301

Hi ppl,

I need an FM to change the date format into the format maintained in the default settings for the user.

E.g.

if the default date format maintained is MM-DD-YYYY, then the system date YYYYMMDD should be conveted to MM-DD-YYYY.

if the default date format maintained is MM/DD/YYYY, then the system date YYYYMMDD should be conveted to MM/DD/YYYY.

and so on for other formats...

Please let me know if there is any standard FM or any logic to achieve this.

Regards.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,082

I'd go for the very nice simple WRITE statement:


data date_string type char10.
write sy-datum to date_string.
write / date_string.

This will automatically apply the user settings. No need for overhead of function/method calls...

7 REPLIES 7
Read only

sarbajitm
Contributor
0 Likes
2,082

Hi,

Step 1.

SELECT SINGLE datfm

INTO fp_datefm

FROM usr01

WHERE bname = sy-uname.

Step 2.

TRY.

CALL METHOD cl_abap_datfm=>conv_date_int_to_ext

EXPORTING

im_datint = l_intdate

im_datfmdes = fp_datefm

IMPORTING

ex_datext = l_extdate. "External Format

CATCH cx_abap_datfm_format_unknown.

WRITE:/ text-033.

ENDTRY.

Regards.

Sarbajit.

Read only

Former Member
0 Likes
2,082

hi

try the function module CONVERT_DATE_TO_INTERN_FORMAT.

hope this helps

Regards

Ritesh

Read only

0 Likes
2,082

Hi sarbajit,

The class mentioned by you does not exist in our system.

Hi Ritesh,

The FM works the other way round. It converts MM-DD-YYYY to YYYYMMDD.

Please let me know if there isany other solution possible.

Regards,

Read only

Former Member
0 Likes
2,082

Hi,

You can try this way.

1.Execute the FM SLS_MISC_GET_USER_DATE_FORMAT to the format of the USER.

2.Then format the date according to it.

You can use FM HR_IN_GET_DATE_COMPONENTS to get the components

else you can use the below logic.

IDATE will have the date in internal format i.e. "YYYYMMDD".

DAY = IDATE+6(2).

MONTH = IDATE+4(2).

YEAR = IDATE+0(4).

Thanks & Regards,

Vamsi.

Read only

Former Member
0 Likes
2,083

I'd go for the very nice simple WRITE statement:


data date_string type char10.
write sy-datum to date_string.
write / date_string.

This will automatically apply the user settings. No need for overhead of function/method calls...

Read only

0 Likes
2,082

Hi Harald,

Thanks a million.

This is an awesome solution. I am surprised I was not aware about this yet.

I hope it will work under all scenarios and even after system upgrades. Please let me know if there are any exceptions.

Thanks.

Read only

0 Likes
2,082

I've been using this since release 3.1 (didn't work on SAP before that) and it always worked fine. I'd say that this approach is bound to be pretty safe for upcoming release upgrades...