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

How to calculate time difference,

Former Member
0 Likes
25,362

Hi,

I want to calculate time difference from start date, start time, end date and end time.

The time difference result should be hh:mm:ss.

I know there are plenty of function modules available but I dont have any ABAP experience.

Can some one please provide me the code to do this.

Thank you,

sam

11 REPLIES 11
Read only

Former Member
0 Likes
5,824

Hi

By writing a small program you can calculate the actual time in HHMMSS format

check this fun module which gives the difference in days

<b>HR_HK_DIFF_BT_2_DATES</b>

got SE37

and search for the different fun modules and try

Regards

Anji

Read only

0 Likes
5,824

Thanks for the reply but days is not useful to me.

I need code to give me time difference in hh:mm:ss format..

Read only

Former Member
0 Likes
5,824

hi Sam

use fm

FUNCTION C14Z_CALC_DATE_TIME.

to get in X minutes and then u can calculate according to requirement.

Regards

Avi

Read only

0 Likes
5,824

god function

Read only

0 Likes
5,824
Erol Caglar cf my answer
Read only

Former Member
0 Likes
5,824

Hi Sam,

Check this code,

DATA: EDAYS LIKE VTBBEWE-ATAGE,

EMONTHS LIKE VTBBEWE-ATAGE,

EYEARS LIKE VTBBEWE-ATAGE,

TIME1 TYPE I.

PARAMETERS: FROMDATE LIKE VTBBEWE-DBERVON,

TODATE LIKE VTBBEWE-DBERBIS DEFAULT SY-DATUM.

call function 'FIMA_DAYS_AND_MONTHS_AND_YEARS'

exporting

i_date_from = FROMDATE

i_date_to = TODATE

  • I_FLG_SEPARATE = ' '

IMPORTING

E_DAYS = EDAYS

E_MONTHS = EMONTHS

E_YEARS = EYEARS.

WRITE:/ 'Difference in Days ', EDAYS.

WRITE:/ 'Difference in Months ', EMONTHS.

WRITE:/ 'Difference in Years ', EYEARS.

INITIALIZATION.

FROMDATE = SY-DATUM - 60.

"Then Multiply 24 to EDATS to get the Hours.

TIME1 = EDATS * 24.

Thanks,

Reward If Helpful.

Read only

Former Member
0 Likes
5,824

hi sam,

you can also use SD_DATETIME_DIFFERENCE : which give the difference in Days and Time for 2 dates.

regards,

ritika

Read only

Former Member
5,824

Hi,

The below code will give you only hours and minutes.

DATA:v_diff TYPE tvro-fahztd.

CALL FUNCTION 'SD_CALC_DURATION_FROM_DATETIME'

EXPORTING

i_date1 = '20071001'

i_time1 = '102000'

i_date2 = '20071009'

i_time2 = '114000'

IMPORTING

e_tdiff = v_diff.

WRITE: v_diff.

Read only

Former Member
0 Likes
5,824

hi Sam,

try it:

data: strdate like sy-datum value '20070901',

enddate like sy-datum value '20070902',

strtime like sy-uzeit value '201001',

endtime like sy-uzeit value '201000',

date(10),

time(10),

hour(4),

modhour(4),

min(2),

modmin(2),

sec(2).

date = enddate - strdate.

time = endtime - strtime.

write: date, / time.

time = time + ( date * 24 * 3600 ).

modhour = time mod 3600.

hour = ( time - modhour ) / 3600.

modmin = modhour mod 60.

min = ( modhour - modmin ) / 60.

sec = modmin.

concatenate hour ':' min ':' sec into time.

write: / time.

Regards

Allan Cristian

Read only

Sandra_Rossi
Active Contributor
5,824

There are only 2 officially-supported ways:

Either use CL_ABAP_TSTMP=>SUBTRACT (2 timestamps) as shown here: https://stackoverflow.com/questions/10539133/difference-between-2-timestamps

or CL_ABAP_TSTMP=>TD_SUBTRACT (2 pairs date/time) as shown here: https://stackoverflow.com/questions/12582233/how-to-calculate-time-difference-in-minutes-with-two-in...