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: 

Calculate Date from Hours

Former Member
0 Kudos

Is there a standard SAP function module that calculates a date using a specific number of hours?

For example:

The expected GR date is 4 days and 11 hours from the PGI date. So we would like to use the functionality to add 107 hours to the PGI date to recieve the Expected GR date.

thanks,

brian

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi brian,

1. The logic is simple.

2. For this purpose, i have an independent

subroutine - FORM,

in which we pass the hours,

and we get back days, and hours.

107 hours = 4 days , 11 hours

3. just copy paste in new program

4.

report abc.

*----


DATA : DAYS TYPE I.

DATA : HRS TYPE I.

*----


parameters : h type i DEFAULT '107'.

perform getfigure using H CHANGING DAYS HRS.

WRITE : DAYS , HRS.

*----


  • FORM

*----


form getfigure using phours changing days hrs.

DATA : MYDAYS TYPE P DECIMALS 2.

MYdays = phours / 24.

DAYS = TRUNC( MYDAYS ).

hrs = phours mod 24.

endform.

regards,

amit m.

10 REPLIES 10

ferry_lianto
Active Contributor

Hi Brian,

Please check this FM <b>C14Z_CALC_DATE_TIME</b>.

Hope this will help.

Regards,

Ferry Lianto

Please reward point if helpful.

0 Kudos

Also check !

SD_CALC_DURATION_FROM_DATETIME

Hope this’ll give you idea!!

<b>P.S award the points.</b>

Good luck

Thanks

Saquib Khan

"Some are wise and some are otherwise"

0 Kudos

hi Brain,

Check FM <b>C14Z_CALC_DATE_TIME</b>...

Hope this helps,

Regards,

Santosh

0 Kudos

I guess there is a bug in the function module. C14Z_CALC_DATE_TIME..See the results to corraborate.

_______________________________________________________

Import parameters Value

I_ADD_SECONDS 3.600

I_UZEIT 10:00:00

I_DATUM 2006/01/01

Export parameters Value

E_DATUM 2006/01/02

E_UZEIT 10:00:00

Former Member
0 Kudos

Hi brian,

1. The logic is simple.

2. For this purpose, i have an independent

subroutine - FORM,

in which we pass the hours,

and we get back days, and hours.

107 hours = 4 days , 11 hours

3. just copy paste in new program

4.

report abc.

*----


DATA : DAYS TYPE I.

DATA : HRS TYPE I.

*----


parameters : h type i DEFAULT '107'.

perform getfigure using H CHANGING DAYS HRS.

WRITE : DAYS , HRS.

*----


  • FORM

*----


form getfigure using phours changing days hrs.

DATA : MYDAYS TYPE P DECIMALS 2.

MYdays = phours / 24.

DAYS = TRUNC( MYDAYS ).

hrs = phours mod 24.

endform.

regards,

amit m.

0 Kudos

Amit,

This is great code, however it just brings back the days and hours... how would i then apply that to the current date and time to get the expected date and time?

-brian

0 Kudos

I haven't look at the code closely, but if it is bring back the days and hours, all you should need to do is add the days to the date, and the hours to the time. If date is typed as sy-datum and time is typed as sy-uzeit, then adding the values to them will work. The system will handle date and times for you.

Of course this won't work if the hours pushs you into the next day. So you would have to handle that by checking and adding one to the date.

Regards,

Rich Heilman

0 Kudos

Here is the sample code a little modified. This will handle when the hours push to the next day.



report abc.

data : days type i.
data : hrs(2) type c.

data: t type sy-uzeit value '113500',
      d type sy-datum value '20060606'.
data: test_time type sy-uzeit.
data: conv_hrs type sy-uzeit.

parameters : h type i default '110'.

perform getfigure using h changing days hrs.
write : days , hrs.

d = d + days.

conv_hrs+0(2) = hrs.

test_time = conv_hrs + t.
if test_time < t.
  d = d + 1.
endif.
t = t + conv_hrs.

write:/ d, t.


*---------------------------------
* FORM
*---------------------------------

form getfigure using phours changing days hrs.

  data : mydays type p decimals 2.
  mydays = phours / 24.
  days = trunc( mydays ).
  hrs = phours mod 24.

endform.

Regards,

Rich Heilman

0 Kudos

execellent, i was just coding your first post when i saw that you responded with your changes... thank you very much

any idea on how to relate to the factory calendar?

former_member181962
Active Contributor
0 Kudos

The best way is to use the FM ADD_TIME_TO_DATE

Sample Usage:

<b>

CALL FUNCTION 'ADD_TIME_TO_DATE'

EXPORTING

i_idate = wa_starting_date

i_time = wa_time

i_iprkz = wa_iprkz

  • I_RDMHD =

IMPORTING

o_idate = wa_return_date

EXCEPTIONS

invalid_period = 1

invalid_round_up_rule = 2

internal_error = 3

OTHERS = 4

.</b>

Regards,

Ravi