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: 

Date and Time Diff in HH:MM : Issue Critical

Former Member
0 Kudos

Hello Guys,

My Report Requirement is as follows: -

I have 2 dates D1 and D2. ( yyyymmdd)

also 2 times T1 and T2. "( hh:mm:ss)

I want the diff in hh:mm.

Also after getting the result in HH:MM. for each record.

I want the report to display the result by DATE - WISE / month - wise / year -wise and plant-wise ( 3 ) average in HH:MM.

Could you please provide me solution.

thanks,

Kind Regards,

Pratap

11 REPLIES 11

Former Member
0 Kudos

any suggestion guys

0 Kudos

Check function module SD_DATETIME_DIFFERENCE

0 Kudos

Hello Vicenç Lozano ,

I know SD_DATETIME_DIFFERENCE fm which takes date and time and gives the result.

But I want to get the average time in HH:MM, daily ,monthly and yearly.

thanks

pratap

former_member208856
Active Contributor
0 Kudos

Hi,

Check this code, it can help you,

data : date_diff type p, earliest type C, timediff type p.

data : a_new type d, b_new type d.

data : c_new type t, d_new type t.

a_new = '20070701'. "Date format is YYYYMMDD

b_new = '20070730'. "Date format is YYYYMMDD

c_new = '100000'.

d_new = '120000'.

CALL FUNCTION '/SDF/CMO_DATETIME_DIFFERENCE'

EXPORTING

date1 = a_NEW

time1 = c_new

date2 = b_NEW

time2 = d_new

IMPORTING

DATEDIFF = date_diff

TIMEDIFF = timediff

EARLIEST = earliest

EXCEPTIONS

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

write : / date_diff.

write : / timediff.

write : / earliest.

Reward points, if helpful,

Sandeep Kaushik

Former Member
0 Kudos

use this piece of code

DATA: l_start TYPE t,

l_end TYPE t,

l_startdate TYPE d,

l_enddate TYPE d,

l_hours TYPE p DECIMALS 2.

l_start = p_starttime.

l_end = p_endtime.

l_startdate = p_startdate.

l_enddate = p_enddate.

l_hours = ( ( l_enddate - l_startdate ) * 24

+ ( l_end - l_start ) / 3600 ).

or refer the FM's

<b>RP_CALC_DATE_IN_INTERVAL

SD_DATETIME_DIFFERENCE</b>

regards,

srinivas

<b>*reward for useful answers*</b>

0 Kudos

I'm sorry, but this piece of code needs a lot of tuning to prevent mistakes with the time values. It will be more accurate using the provided FM who cares about those things.

About the expected result, I cannot understand what do you mean with "average" hh:mm and so...

If you want the difference in hours, just use the FM and do a single calculation:

hours = DATEDIFF * 24 + TIMEDIFF

minutes = hours * 60

days = hours / 24 and so...

But still thinking I'm missing something about your requirements. Let's try with an example: you have " Jan 1, 2007 00:00:01 and Jan 10, 2007 10:00:01", the FM gives you

DATEDIFF = 9 (days)

TIMEDIFF = 10 (hours)

EARLIEST = 1

What would you expect with those dates?

0 Kudos

Hello Vicenç Lozano,

Let me make more clear about my Issue.

I have a table with has d1 and d2 and t1 and t2.

I want the diff between them in only HH:MM, using any of the function modules.

Now my report requirement.

I want to display the report with yearly , monthly and daily information -

let say my boss wants to compare Average time ( HH:MM) between June and July of 2007.

the report should be

year ++++++++ month ++++++ daily ++++++++ rec#++++++++ diff

2007++++++++ June++++++++ 01++++++++ 123++++++++ 31.36

2007++++++++ June++++++++ 01++++++++ 124++++++++ 11.23

2007++++++++ June ++++++++ 03 ++++++++ 152 ++++++++ 18.21

2007++++++++ July ++++++++ 01++++++++ 157 ++++++++ 12.27

2007++++++++ July++++++++ 08 ++++++++ 124 ++++++++ 15.33

now when you look at the above report layout.....what i want is to display the average time ( diff) for month of june and july.

so the average time diff of the month of june will be ( total time / record count ). same for july.

so the result should be for

JUNE - (32.36 + 11.23 + 18.21) / 3

JULY - (12.27+15.33) / 2

also it should convert the decimals (MM) into hours after 60.

hope you got my issue.

thanks,

pratap.

0 Kudos

Let's assume you have the difference in hours. Let's suppose you've got the logic to calculate the average in "normal" numbers (eg, through a LOOP-ENDLOOP).

You just need to convert the fractionary part of the hours value into minutes.

Be careful about the fact you CANNOT use a TIMS value, because it's ranged between 000000 and 235959, and you cannot get more than 24 hours. You must make your own type or just use any numerical one.

The number of hours are easy to get, you just need to use a TRUNC over your hours value. The decimals are easy to find now, because decimal minutes = avg_hours / TRUNC( avg_hours). Now you just need to convert them to minutes.

minutes = decimal_minutes * 60, but you want them as a two decimal positions of your output. Divide it by 100.

* Let's suppose:
* avg_hours: value of the calculated average time
* output: value wanted to be shown
DATA: hours TYPE p DECIMALS 2,
      d_min TYPE p DECIMALS 2,
* You can do all the calculations using a single value, but we are trying to get clear
      mins  TYPE p DECIMALS 2.
  hours = TRUNC( avg_hours ).
  d_min = avg_hours - hours.
  mins  = d_min * 60 / 100. "you can put d_min * '0.6'
* And the calculated and transformed average times:
  output = hours + mins.

0 Kudos

Hi Vicenç Lozano ,

thanks for the reply.I have used your logic.

But the result (output ) .....its rounding off the values.

say if desired output is 83:39 (HH:MM), using your code its giving me 84.

its displaying as 84:00, for the values.

please sugguest.

Thanks,

Pratap.

0 Kudos

How have you defined your "output" var? Maybe you are using some kind of type that rounds (up) the numbers after the summatory.

Try with output defined as P DECIMALS 2.

And if you did it and still not working, try to put those numbers (60, 100 and/or 0,6) in P DECIMALS 2 variables, maybe the system is getting them as integer and converting the whole number to integers...

(sorry for the delay, I have to sleep sometimes :P)

Former Member
0 Kudos

data:DATE_FROM LIKE LTAK-BDATU,

DATE_TO LIKE LTAK-BDATU,

TIME_FROM LIKE LTAK-BZEIT,

TIME_TO LIKE LTAK-BZEIT,

DELTA_TIME LIKE MCWMIT-BE_AE,

DELTA_UNIT LIKE MCWMIT-LZEIT.

DATE_FROM = SY-DATUM.

DATE_TO = DATE_FROM + 1.

TIME_FROM = '220000'.

TIME_TO = '060000'.

CALL FUNCTION 'L_MC_TIME_DIFFERENCE'

EXPORTING

date_from = DATE_FROM

date_to = DATE_TO

TIME_FROM = TIME_FROM

TIME_TO = TIME_TO

IMPORTING

DELTA_TIME = DELTA_TIME

DELTA_UNIT = DELTA_UNIT

EXCEPTIONS

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

WRITE:/ DELTA_TIME , DELTA_UNIT.

2. to check //If a request is made Local time (0TIME) between 10PM to 6AM

you can use this logic..

DATA : VAL LIKE SY-UZEIT.

VAL = SY-UZEIT. " if u have this

IF VAL BETWEEN TIME_FROM AND '240000' OR

VAL BETWEEN '000000' AND TIME_TO.

WRITE:/ 'IN THE INTERVAL'.

ELSE .

WRITE:/ 'NOT IN THE INTERVAL'.

ENDIF.