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: 

To calculate difference between two dates?

Former Member
0 Kudos

Hi All,

Is there any function module or BAPI to calculate difference between two dates which are in timestamp format(ie., Decimal 15)?

Thanks & Regards,

Anil

1 ACCEPTED SOLUTION

former_member184569
Active Contributor
0 Kudos

Hi Anil,

You want the difference in what unit?

As I told before you can use the function module CCU_TIMESTAMP_DIFFERENCE. This will give the difference in seconds.

You can convert it to years or days as per your requirement.

data : dt1 like CCUPEAKA-TIMESTAMP,

dt2 like CCUPEAKA-TIMESTAMP,

diff type i,

days type i,

years type i.

dt1 = '20050101111111'.

dt2 = '20040101112222'.

CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'

EXPORTING

timestamp1 = dt1

timestamp2 = dt2

IMPORTING

DIFFERENCE = diff

.

write : / 'Difference in seconds', diff.

*If you want diff in days

diff = diff / 86400.

write : / 'Difference in days', diff.

*If you want diff in years and days,

years = diff / 365.

days = diff mod 365.

Write : / 'Difference is ' , years ,'years', days , 'days'.

Thanks,

Susmitha

Note : Please award points and close thread if your problem is solved.

10 REPLIES 10

Simha_
Employee
Employee
0 Kudos

Hi,

Use this F.M <b>COMPUTE_YEARS_BETWEEN_DATES</b>.

<b>HR_HK_DIFF_BT_2_DATES</b>

May be this will be useful..

Cheers,

Simha.

Former Member
0 Kudos

Hi simha,

i tried using FM HR_HK_DIFF_BT_2_DATES, but this FM doesnt give difference when the dates are in timestamp format(date+time).

Thanks & Regards,

Anil.

Former Member
0 Kudos

Use Function module SD_DATETIME_DIFFERENCE to get the result.

0 Kudos

hi,

Try these..

SD_DATETIME_DIFFERENCE

SD_CALC_DURATION_FROM_DATETIME

Cheers..

Simha.

former_member184569
Active Contributor

The difference between the two dates can be found by just subtracting the two.

YOu dont require FM for that,

data : d1 type sy-datum,

d2 type sy-datum,

n type i.

d1 = '20050101'.

d2 = '20060101'.

n = d2 - d1.

write n.

n will be 365.

Hope that It helped

Thanks,

Susmitha

Thanks,

0 Kudos

This message was moderated.

former_member184569
Active Contributor
0 Kudos

Sorry,

Didnt notice timestamp.

You can use this FM

CCU_TIMESTAMP_DIFFERENCE

pradiptakumar_mishra
Participant
0 Kudos

Use FM 'HR_HK_DIFF_BT_2_DATES' .

Hope it'll solve your issue.

Former Member

<b>GOt this info from a link:</b>

I am working on a program that needs to show number of days between 2 dates. When I scanned the function library, I only found a function to give you the number of years between dates. I can probably code this in ABAP but does anyone know if a function exists to do this.

I wrote this example for you. I think this is what you need.

DATA: DATE_1 LIKE SY-DATUM,  
             DATE_2 LIKE SY-DATUM. 
             DATA DAYS TYPE I. 

DATE_1 = SY-DATUM. 
DATE_2 = SY-DATUM + 65. 
DAYS = DATE_2 - DATE_1. 
WRITE:/ 'DATE_2=',DATE_2,'DATE_1=',DATE_1,'DAYS=',DAYS

.

Run this code and then you will understand.

Also refer this link

http://www.sap-img.com/fu004.htm

REPORT ZDATEDIFF.

DATA: EDAYS   LIKE VTBBEWE-ATAGE,
      EMONTHS LIKE VTBBEWE-ATAGE,
      EYEARS  LIKE VTBBEWE-ATAGE.

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.

Reward points if this solves ur problem.

former_member184569
Active Contributor
0 Kudos

Hi Anil,

You want the difference in what unit?

As I told before you can use the function module CCU_TIMESTAMP_DIFFERENCE. This will give the difference in seconds.

You can convert it to years or days as per your requirement.

data : dt1 like CCUPEAKA-TIMESTAMP,

dt2 like CCUPEAKA-TIMESTAMP,

diff type i,

days type i,

years type i.

dt1 = '20050101111111'.

dt2 = '20040101112222'.

CALL FUNCTION 'CCU_TIMESTAMP_DIFFERENCE'

EXPORTING

timestamp1 = dt1

timestamp2 = dt2

IMPORTING

DIFFERENCE = diff

.

write : / 'Difference in seconds', diff.

*If you want diff in days

diff = diff / 86400.

write : / 'Difference in days', diff.

*If you want diff in years and days,

years = diff / 365.

days = diff mod 365.

Write : / 'Difference is ' , years ,'years', days , 'days'.

Thanks,

Susmitha

Note : Please award points and close thread if your problem is solved.