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

date difference

Former Member
0 Likes
1,336

hi,

how to calculate difference between two dates , please give me solution

.

regards,

jai

10 REPLIES 10
Read only

Former Member
0 Likes
1,242

Hi,

PLEASE CHECK OUT THE BELOW PROGRAM IT MIGHT HELP YOU

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.

also check out the below program to fine date difference

data: d1 like sy-datum,

d2 like sy-datum.

data: diff type i.

d1 = '20060331'.

d2 = '20060220'.

diff = d1 - d2.

write : / diff .

Also chek these FMs:

'CSCP_PARA1_GET_PERIODS'

HR_99S_INTERVAL_BETWEEN_DATES(Months, Years and days)

COMPUTE_YEARS_BETWEEN_DATES

EHS_CALC_YEARS_BETWEEN_DATES

DAYS_BETWEEN_TWO_DATES

MONTHS_BETWEEN_TWO_DATES

For No of week days between two dates please check out the below program it might help you

report zrich_0003.

data: begin of itab occurs 0,

datum type sy-datum,

end of itab.

data: weekday like dtresr-weekday.

data: number_lines type i.

parameters: p_sdatum type sy-datum,

p_edatum type sy-datum.

itab-datum = p_sdatum.

append itab.

do.

if itab-datum = p_edatum.

exit.

endif.

itab-datum = itab-datum + 1.

call function 'DATE_TO_DAY'

exporting

date = itab-datum

importing

weekday = weekday.

if weekday = 'Sat.'

or weekday = 'Sunday'.

continue.

endif.

append itab.

enddo.

describe table itab lines number_lines.

write:/ 'Number of days between dates is', number_lines.

**********please reward points if the information is helpful to you*************

Read only

Former Member
0 Likes
1,242

Hi,

Try this,

GOt this info from a link:

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.

Thanks,

Reward If Helpful.

Read only

Former Member
0 Likes
1,242

if your variables are date type then you can use subtraction for them...

data : date1 like sy-datum,

date2 like sy-datum,

diff type i.

date1 = '20070101'.

date2 = '20070215'.

diff = date2 - date1.

or use fm

DAYS_BETWEEN_TWO_DATES

or

FIMA_DAYS_AND_MONTHS_AND_YEARS

regards

shiba dutta

Read only

Former Member
0 Likes
1,242

Hi Jai,

Follow the below code for date difference.

data : d1 type sy-datum,

d2 type sy-datum,

d3(4) type n.

d1 = sy-datum.

d2 = sy-datum + 10.

d3 = d2 - d1.

write : d3.

Regards,

Anbalagan

Read only

Former Member
0 Likes
1,242

Hi,

Use the function module

HR_SGPBS_YRS_MTHS_DAYS

Read only

Former Member
0 Likes
1,242

Hi,

This FM may be helpful

DAYS_BETWEEN_TWO_DATES

Regards,

Read only

Former Member
0 Likes
1,242

Hi,

Try with this:

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.

REgards,

Bhaskar

Read only

Former Member
0 Likes
1,242

Hi,

The following contains what is collected over some time, please go through, and let me know of improvements and suggestions please.

Some confusion about date and time formats remain, though.

I would also like to know the significance of SAP standard functions, and what all times they should be encouraged / avoided.

Though presentation here surely needs improvement, I hope it helps.

1.To get a date from a given date +/- period. Input : Date & Period (No. of Days) Output : Calculated Date

Please go through the Function Module "RELATIVE_DATE_CALCULATE". Remember to enter the date in 'yyyymmdd' format in the start date field.

2.if the days variable is integer and other 2 like sy-datum, the math below yields the number of days to yo..

start date = '23.07.2003'

End date = '23.09.2003'

Days = enddate - Start date.

3. Please go through the Functions Module , it gives date as well as time difference, though U may have to enter time as well.

SD_DATETIME_DIFFERENCE

4. with this FM you can get dates between two dates : Function Module

RP_CALC_DATE_IN_INTERVAL

5. go into SE37 and search on 'difference between dates', one of these 3 keywords or a combination between the 3 will give you a function module.

6. Date Upper & Lower limit: the lower is obviously the 1st of the month. To get the upper use function module END_OF_MONTH_DETERMINE.

7. to get the logon user setting date dynamically : Using offsets move it into a date field,obviously in the format yyyymmdd. then move it into a char(10) field. U get the data in char(10) using the current user's setting.

ex:: w_date type d value '20030822',

w_char(10).

say logon user setting is mm/dd/yyyy.

write w_date to w_char. w_char will have the value 08/22/2003.

You can use the result of the move stmt in bdc table.

8. Check this FM

"/SAPDMC/LSM_DATE_CONVERT "

9.statements:

1. CONVERT DATE f1 INTO INVERTED-DATE f2.

2. CONVERT INVERTED-DATE f1 INTO DATE f2.

Calculates the nine's complement of the internal date format (YYYYMMDD - for more information about internal display, meaning and initial values of types and places it in the field f2.

For example, 19950511 ==> 80049488 or 80049488 ==> 19950511.

In the inverse date format, the most recent date has the numerically smallest value. You can make use of this effect when sorting dates.

10. GET TIME STAMP : returns the timestamp in short or long form. The short form contains the current date and time :

DATA:

s_tst TYPE timestamp,

l_tst TYPE timstampl,

tzone TYPE timezone.

GET TIME STAMP FIELD s_tst. "Short form

GET TIME STAMP FIELD l_tst. "Long form

tzone = 'UTC '. "Time zone

WRITE: / s_tst TIME ZONE tzone,

/ l_tst TIME ZONE tzone.

Then, The short form might then contain the value 19980204163520, while the long form has the value 19980204163520,1234560.

The time stamp can be converted by using CONVERT TIME STAMP and CONVERT DATE

11.Some system fields: DATLO Local date for user DATUT Global date related to UTC (GMT) TSTLO Timestamp (date and time) for user TSTUT Timestamp (date and time) related to UTC (GMT)

12. Check the site http://funciones.sapabap.cc for functions like DATE_CHECK_PLAUSIBILITY Check to see if a date is in a valid format for SAP.

Works well when validating dates being passed in from other systems.

DATE_COMPUTE_DAY Returns a number indicating what day of the week the date falls on. Monday is returned as a 1, Tuesday as 2, etc.

DATE_GET_WEEK will return the week that a date is in.

DATE_IN_FUTURE Calculate a date N days in the future.

DAY_ATTRIBUTES_GET Return useful information about a day. Will tell you the day of the week as a word (Tuesday), the day of the week (2 would be Tuesday), whether the day is a holiday, and more.

F4_DATE for popping up calendar for date help on value request

RH_GET_DATE_DAYNAME return the day based on the date provided

RP_CALC_DATE_IN_INTERVAL Add/subtract years/months/days from a date

RP_LAST_DAY_OF_MONTHS Determine last day of month

WEEK_GET_FIRST_DAY For a given week (YYYYMM format), this function returns the date of the Monday of that week.

Pls reward points.

Regards,

Ameet

Read only

Former Member
0 Likes
1,242

see the below logic ..

REPORT ZEXAMPLE. 
DATA V_DIFF TYPE I. 

PARAMETERS P_BIRTHD LIKE SY-DATUM. 

CALL FUNCTION 'DAYS_BETWEEN_TWO_DATES' 
EXPORTING 
I_DATUM_BIS = SY-DATUM 
I_DATUM_VON = P_BIRTHD 
IMPORTING 
E_TAGE = V_DIFF 
EXCEPTIONS 
DAYS_METHOD_NOT_DEFINED = 1 
OTHERS = 2. 

IF SY-SUBRC EQ 0. 
WRITE:/ V_DIFF, 'DAYS HAVE PASSED SINCE', P_BIRTHD. 
ELSE. 
WRITE:/ 'ERROR IN CALCULATION'. 
ENDIF.

<b>List of Function Modules for Date's finding </b>


DAYS_BETWEEN_TWO_DATES
LEAP_DAYS_BETWEEN_TWO_DATES
HR_SEN_CALE_DAYS_DATE
ISB_DAYS_BETWEEN_TWO_DATES
FIMA_DAYS_BETWEEN_TWO_DATES
FIMA_DAYS_BETWEEN_TWO_DATES_2
FIMA_LEAP_DAYS_BETWEEN_2_DATES 
SD_DATETIME_DIFFERENCE

Give the difference in Days and Time for 2 dates

reward points if it is usefull ....

Girish

Read only

Former Member
0 Likes
1,242

Please try out the below code. Remember that you need to pass the time also alongwith dates or else it gives error.

REPORT YAK_TEST5.

data : dat1 like sy-datum,

dat2 like sy-datum,

time1 like sy-uzeit,

time2 like sy-uzeit,

diff type p.

dat1 = '20070716'.

dat2 = '20070710'.

time1 = '121212'.

time2 = '121212'.

CALL FUNCTION 'SD_DATETIME_DIFFERENCE'

EXPORTING

DATE1 = dat1

TIME1 = time1

DATE2 = dat2

TIME2 = time2

IMPORTING

DATEDIFF = diff.

write 😕 'Date Difference:', diff.