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

difference between dates

Former Member
0 Likes
944

Hi,

can anyone let me know whether there are any function module available to get the number of days between two different dates..

Thanks,

Uma.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
889

Uma,

A Func Mod call will only run slower at execution time.

Follow Rich's syntax - any FM will perform the same logic in the end AND no function pool (an additional program) needs to be loaded into memory. This loading of an additional prog simply slows down a straight-forward process..

Just subtract them.

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
889

No need for a FM.

Data: days type i.

days = end_date - start_date.

Write:/ days.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
889

Hi Uma,

Use this F.M COMPUTE_YEARS_BETWEEN_DATES.

HR_HK_DIFF_BT_2_DATES

May be this will be useful..

or...............

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'.

or...........

you can also use the below fm to find the days between two dates

FM DAYS_BETWEEN_TWO_DATES

Ex:

PARAMETER:p_date1 TYPE dats,

p_date2 TYPE dats.

DATA:lv_diff TYPE i.

CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES'

EXPORTING

i_datum_bis = p_date1

i_datum_von = p_date2

IMPORTING

e_tage = lv_diff

EXCEPTIONS

days_method_not_defined = 1

OTHERS = 2.

IF sy-subrc = 0.

WRITE:/ lv_diff.

ENDIF.

~~Guduri

Read only

Former Member
0 Likes
889

Hai Uma

Go through the following Code

data: begin_date type sy-datum value '20060110',

end_date type sy-datum value '20060125'.

data: idatum type table of sy-datum with header line.

idatum = begin_date.

append idatum.

do.

if idatum = end_date.

exit.

endif.

idatum = idatum + 1.

append idatum.

enddo.

loop at idatum.

write:/ idatum.

endloop.

For Days , Months & Years

DATA: D TYPE I, M TYPE I, Y TYPE I.

CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'

EXPORTING

I_DATE_FROM = DATE1

  • I_KEY_DAY_FROM =

I_DATE_TO = DATE2

  • I_KEY_DAY_TO =

  • I_FLG_SEPARATE = ' '

IMPORTING

E_DAYS = D

E_MONTHS = M

E_YEARS = Y

.

write 😕 D,

/ M,

/ Y.

For No of Sundays & Saturdays

PARAMETERS:

date1 LIKE sy-datum DEFAULT '20060501',

date2 LIKE sy-datum DEFAULT sy-datum.

DATA i .

DATA z TYPE p DECIMALS 0.

DATA cnt TYPE sy-dbcnt.

DATA cnt_su TYPE sy-dbcnt.

DATA cnt_sa TYPE sy-dbcnt.

WRITE: date1, date2.

WHILE date1 LE date2.

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

datum = date1

IMPORTING

wotnr = z.

ADD 1 TO date1.

IF z = 6.

ADD 1 TO cnt_sa.

ELSEIF z = 7.

ADD 1 TO cnt_su.

ENDIF.

ENDWHILE.

WRITE: / 'Sun:', cnt_su, 'Sat:', cnt_sa.

Regards

Sreeni

Read only

Former Member
0 Likes
890

Uma,

A Func Mod call will only run slower at execution time.

Follow Rich's syntax - any FM will perform the same logic in the end AND no function pool (an additional program) needs to be loaded into memory. This loading of an additional prog simply slows down a straight-forward process..

Just subtract them.

Read only

0 Likes
889

Good explaniation, John.

Regards,

RIch Heilman

Read only

0 Likes
889

Thanks - too many developers NEED to use FMs... when it is not truly necessary.