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 two dates

Former Member
0 Likes
4,930

Hi,

how to find the differnce between two dates?(have to get no of days)

date format will be in this format yyyymmdd.

Regards

Ajay

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,019

FM:HR_HK_DIFF_BT_2_DATES

Or

Check this Example..

DATA:DATE1 TYPE D VALUE '20071220'.

DATA:DATE2 TYPE D VALUE '20071120'.

DATA:DAYS TYPE I.

CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES'

EXPORTING

I_DATUM_BIS = DATE1

I_DATUM_VON = DATE2

I_KZ_EXCL_VON = '0'

I_KZ_INCL_BIS = '0'

I_KZ_ULT_BIS = ' '

I_KZ_ULT_VON = ' '

I_STGMETH = '0'

I_SZBMETH = '1'

IMPORTING

E_TAGE = DAYS

EXCEPTIONS

DAYS_METHOD_NOT_DEFINED = 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:/ DAYS

Reward points..

11 REPLIES 11
Read only

Former Member
0 Likes
2,019

Hi,

See the following example code.

**CHANGING DATE FORMAT

&----


*& Report ZJE_DATE

*&

&----


*&

*&

&----


REPORT ZJE_DATE.

data: date(10) type c,

date1 like date,

date2 like date.

date = sy-datum.

write:/ date.

write:/ sy-datum.

write:/ date using edit mask '____:__:__'.

CALL FUNCTION 'CONVERT_DATE_TO_INTERN_FORMAT'

EXPORTING

DATUM = date

DTYPE = date1

IMPORTING

  • ERROR =

IDATE = date2

  • MESSG =

  • MSGLN =

.

write:/ date2.

Read only

Former Member
0 Likes
2,020

FM:HR_HK_DIFF_BT_2_DATES

Or

Check this Example..

DATA:DATE1 TYPE D VALUE '20071220'.

DATA:DATE2 TYPE D VALUE '20071120'.

DATA:DAYS TYPE I.

CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES'

EXPORTING

I_DATUM_BIS = DATE1

I_DATUM_VON = DATE2

I_KZ_EXCL_VON = '0'

I_KZ_INCL_BIS = '0'

I_KZ_ULT_BIS = ' '

I_KZ_ULT_VON = ' '

I_STGMETH = '0'

I_SZBMETH = '1'

IMPORTING

E_TAGE = DAYS

EXCEPTIONS

DAYS_METHOD_NOT_DEFINED = 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:/ DAYS

Reward points..

Read only

Former Member
0 Likes
2,019

hi

here is function module to find the no days ,no of years,no of months between two dates.

HR_SGPBS_YRS_MTHS_DAYS

just check and give me reply

Read only

Former Member
0 Likes
2,019

Hi

You just subtract them

Exp :

PARAMETERS: p_date TYPE dats.

DATA: w_diff TYPE i.

w_diff = p_date - sy-datum.

WRITE w_diff.

Please assign points if helpful.

Read only

0 Likes
2,019

CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

DATE1 = date1

DATE2 = date2

OUTPUT_FORMAT = '02'

IMPORTING

  • YEARS =

  • MONTHS =

DAYS = diff

EXCEPTIONS

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

Read only

prasanth_kasturi
Active Contributor
0 Likes
2,019

hi use

HR_HK_DIFF_BT_2_DATES - Calculate the days, months and years between 2

dates.

take care date 3 must be greater than date4 for this fm

parameters : date3 type P0001-BEGDA,

date4 type P0001-BEGDA.

data days type P0347-SCRDD.

caLL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

date1 = date3

date2 = date4

OUTPUT_FORMAT = '02'

IMPORTING

  • YEARS =

  • MONTHS =

DAYS = days.

write : days.

or use

FIMA_DAYS_AND_MONTHS_AND_YEARS-Get number of days. Year, Months between two dates.

parameters : date1 type sy-datum,

date2 type sy-datum.

data e_days type VTBBEWE-ATAGE.

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 = e_days

  • E_MONTHS =

  • E_YEARS =

.

regards

prasanth

Read only

former_member386202
Active Contributor
0 Likes
2,019

Hi,

USe FM /SDF/CMO_DATETIME_DIFFERENCE

Regards,

PRashant

Read only

Former Member
0 Likes
2,019

Hi,

Have a look on the following code. It is by using function modules

data: num type c.

CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES'

EXPORTING

i_datum_bis = '20080516'

i_datum_von = '20080510'

IMPORTING

E_TAGE = num

EXCEPTIONS

DAYS_METHOD_NOT_DEFINED = 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:/ num.

Reward,if useful.

Thanks,

Chandu

Read only

Former Member
0 Likes
2,019

Hi,

use this function module,

CALL FUNCTION 'HR_SGPBS_YRS_MTHS_DAYS'

EXPORTING

beg_da = s_date-low

end_da = s_date-high

IMPORTING

no_cal_day = d.

Regards,

kk.

Read only

Former Member
0 Likes
2,019

Hi,

1. Please search the forum, this question is asked almost every day.

2. There are a number of function modules that will do this, but by far the easiest is to subtract one date field from the other. The result is the number of days.


DATA: W_DATE1 TYPE DATS,
           W_DATE2 TYPE DATS.

DATA: RESULT(4) TYPE C.

RESULT = W_DATE1 - W_DATE2.

Regards,

Nick

Read only

Former Member
0 Likes
2,019

Hi,

See below eg:

data: d1(8) type n,

d2(8) type n,

r type n.

d1 = '20080501'.

d2 = '20080510'.

r = d2 - d1.

write: r.

I got the result 9.

Regards

Ganesh