Skip to Content
-2

Date conversion using WRITE statement

Apr 03, 2017 at 10:53 AM

180

avatar image

Hi Experts,

In a report I need to convert date (YYYYMMDD) to another format DD/MM/YY using WRITE statement as below. For one of the record which has value 20170403 it is converting wrongly as 17/04/03. the conversion is done using as below.

WRITE lv_fkdat to lv_date DD/MM/YY.

The type of lv_date is declared as char8.

However for other records it is working fine. If I change the value in debugger, it is working correctly.

Thanks and Regards,

Aditya

date.jpg (104.9 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Richard Harper Apr 03, 2017 at 11:52 AM
3

The Write statement will generally get things right (!) if the data has a domain that has a conversion exit attached to it and it's the type of lv_fkdat that you need to worry about.

If lv_fkdat has a dats type (sy-datum or whatever) then writing that will result in the correct format for the user. Otherwise have a look at the date convert function modules such as CONVERT_DATE_TO_EXTERNAL.

Rich

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Richard,

Thanks for your answer. Does it mean that the conversion routine takes precedence over the WRITE statement ?

It happened only with this test case. However for another date, if I changed the value, it is considering the WRITE statement format. Request you to help me understand this ambiguity.

Once again thanks foryour time.

Thanks and Regards,

Aditya

0

The Write statement uses the conversion exit if the data element is specified with the correct domain.

Rich

0
Horst Keller
Apr 03, 2017 at 11:52 AM
2

The separators and the sequence depend on the formatting setting of the language environment that is predefined by fixed values in the user master record and can be influenced by SET COUNTRY. Is your result user dependend or is there any SET COUNTRY before WRITE TO?

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Horst,

Thanks for your reply, but we are not using any user-specific setting or country speciifc formatting in the report.

It happened only with this test case. However for another date, if I changed the value, it is considering the WRITE statement format.

Once again thanks for your time.

Regards,

Aditya

0

"Thanks for your reply, but we are not using any user-specific setting or country speciifc formatting in the report."

Of course you do. Any WRITE statement using these date options does it - if not overridden by a conversion routine as Rich pointed out.

1
John Vo Apr 04, 2017 at 03:54 AM
2

I'm not sure in your program contain any Set country or be effected from user profile.

Why don't you try to split to day month and year then combine the format that your want.

day = lv_from_date +6(2).

month = lv_from_date+4(2).

year = lv_from_date+2(2).

Thank,

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Agreed - KISS principle: if one doesn't master ABAP, just use:

lv_date = |{ lv_fkdat+6(2) }/{ lv_fkdat+4(2) }/{ lv_fkdat+2(2) }|. " CCYYMMDD -> DD/MM/YY
2

Use substring instead of +x(y)?

0

+() is obsolete ? :)

0

Substring is more obvious. I like obvious code.

0

I prefer a structure - move your string to the struc and then read the components, or move your data to the components and read the string.

0