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 convert decimal years in Year, months, days

Former Member
0 Kudos

Hi at all,

I have the following problem:

I need to convert a decimal years in Years, months and days, for example

If I have 4,65 years I want to get 4 years, 7 months and 29 days.

Someone of you know some FM that do this or how do this??

Thank you in advance

8 REPLIES 8

JonathanM
Contributor
0 Kudos

Hi Tony,

You should take a look at those FM:

  • HR_HK_DIFF_BT_2_DATES
  • RE_ADD_MONTH_TO_DATE  

As a year is twelve months and days can be added directly to your variable.

Best regards,

Jonathan

Former Member
0 Kudos

Hi Jonathan,

The first FM HR_HK_DIFF_BT_2_DATES made the difference two dates isn´t that I want.

The second FM RE_ADD_MONTH_TO_DATE add only a mounth to a date, but my problem is another:

I have this result 4,65 years. I would like to get this information

4 years

(0,65 years) correspond to 7,8 months

(0,08 mounth) correspond to 29 days.

A the end the result should be

4 years 7mounths and 29 days.

0 Kudos

Hi Tony,

Most of the time this is done the other way around... You want to get the difference between 2 dates or add an period interval from a date.

In your case, you have an interval period numeric that you want to split in YMD.

You can do it like:

DATA: VAL TYPE P DECIMALS 2.

DATA: Y TYPE P DECIMALS 2,

       M TYPE P DECIMALS 2,

       D TYPE P DECIMALS 2.

VAL = '4.65'.

Y = VAL DIV 1. "Y=4

VAL = VAL MOD 1.

VAL = VAL * 12.

M = VAL DIV 1. "M=7

D = VAL MOD 1.

"D=???

D = D * 31. "how many days in month?

But as you don't start with a date, how do we know how much days are in a month...?

Best regards,

Jonathan

0 Kudos

Hi Tony,

I have done some checking - it seems that there is no standard function module that meets your request.

You can see the main FMs which deal with dates in the following link:

http://wiki.sdn.sap.com/wiki/display/ABAP/Function+Module+related+on+Date+calculations

Alternatively, you can ask you ABAPer to develop such a FM - it's quite easy.

Regards,

Liran

Former Member
0 Kudos

Hi Liran,

thank you for the answer, can you explain me better  how do  :

ask you ABAPer to develop such a FM - it's quite easy.

Thank you.

0 Kudos

Hi Tony,

It shouldn't be so complicated:

Lets say the X is your decimal number and lets assume that there are 30 days per month as average. so -

* The number of years is retrieved by: FLOOR (x)

* The number of months is retrieved by: FLOOR [ ( X - FLOOR(X) ) * 12 ]

* The number of days is retrieved by: FLOOR  of the result { [ X - FLOOR(X) ] * 12 - the calculated value for number of months } * 30.

Regards,

Liran

Former Member
0 Kudos

Thank you Jonathan,

your code is ok the only question is that this calculation isn´t perfectly correct because I don´t know if consider days of 30 or 31 but i need this information only to show a avarage age for some element in a Queue. I hope that is ok...

Last question, after this calculation, if I want to round the value because i don´t want to show the decimal value but an integer, I have to use the rounding function of abap or i can direclty assign the value to integer?

Thank you!!

0 Kudos

Hi Tony,

Yes, you can move it to an integer or a type P with no decimals to be rounded.

This will follow the "regular rounding".

Otherwise if you want specific rounding, like always to upper value etc, you'll have to work with abap function like trunc(), ceil(), floor(), trunc(), frac() etc. You have examples if you consult the help (F1 on the key word).

Best regards,

Jonathan