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

CHALLANGE : EXPLAIN THIS CODE!!!

Former Member
0 Likes
1,159

HELLO TO ALL EXPERTS,

HERE IS A PIECE OF SIMPLE CODE.

KINDLY EXPLAIN.

_____________________________________________________________________

<b>



*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(DATE1) LIKE  PA0000-BEGDA
*"     VALUE(MODIFY_INTERVAL) DEFAULT ' '
*"     VALUE(DATE2) LIKE  PA0000-BEGDA
*"  EXPORTING
*"     VALUE(YEARS_BETWEEN_DATES)
*"  EXCEPTIONS
*"      SEQUENCE_OF_DATES_NOT_VALID
*"----------------------------------------------------------------------

CASE MODIFY_INTERVAL.
  WHEN '+'. DATE1 = DATE1 - 1.
  WHEN '-'. DATE1 = DATE1 + 1.
  WHEN OTHERS.
ENDCASE.

IF DATE1+4(4)  EQ '0229' AND
   DATE2+4(4) EQ '0228'.
   DATE2 = DATE2 + 1.
  IF DATE2+4(4) EQ '0229'.
   DATE2 = DATE2 - 1.
  ENDIF.
ENDIF.


IF DATE1 LE DATE2.
  IF DATE1+4(4) LE DATE2+4(4).
    YEARS_BETWEEN_DATES = DATE2(4) - DATE1(4).
  ELSE.
    YEARS_BETWEEN_DATES = DATE2(4) - DATE1(4) - 1.
  ENDIF.
ELSE.
  YEARS_BETWEEN_DATES = 0.
  RAISE SEQUENCE_OF_DATES_NOT_VALID.
ENDIF.</b>

_____________________________________________________________________

THANKS!!

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,109

Hi,

This code is from the FM COMPUTE_YEARS_BETWEEN_DATES for calculating years between days.

U can see the same in debugging mode.

When Interval = + the starting date + 1.

If interval = - then starting date -1.

If teh date1 starting date is equal to 29th Feb and End date is 28th Feb add one to end date.

If date2 i.e end date is equal to 29th Feb subtract one from that.

If date1 is less than date2 find teh difference between date1 and date2.

If date2 is less tahn date1 find teh difference between date1 and date2 and subtract one from them.

Hope this is clear.

U can find this in debugging mode.

9 REPLIES 9
Read only

Former Member
0 Likes
1,109

What is the problem?? It caculates the number of yearts between 2 dates...

Regards,

John.

Read only

Former Member
0 Likes
1,109

Hi tejas,

Looks like it is a function module which computes the number of years between two dates.

Regards,

Ravi

Read only

Former Member
0 Likes
1,110

Hi,

This code is from the FM COMPUTE_YEARS_BETWEEN_DATES for calculating years between days.

U can see the same in debugging mode.

When Interval = + the starting date + 1.

If interval = - then starting date -1.

If teh date1 starting date is equal to 29th Feb and End date is 28th Feb add one to end date.

If date2 i.e end date is equal to 29th Feb subtract one from that.

If date1 is less than date2 find teh difference between date1 and date2.

If date2 is less tahn date1 find teh difference between date1 and date2 and subtract one from them.

Hope this is clear.

U can find this in debugging mode.

Read only

0 Likes
1,109

To ALL,

Thanks for your kind and appreciate efforts!

But I'm still not clear with this code.

I want to know how each and every syntax works. what are the inputs and outputs of each syntax.

Kindly Help!!

Thanks!!

Read only

0 Likes
1,109

Hi,

This we cant expalin by typing, u have to debug the code by passing some values.

Put a breakpoint at the starting and see.

For single step execution press F5 and click on date1 and date2 to see how the values are in debugging.

If you see line by line in debuggin u can be more clear.

Pass Feb 29 and Feb 28 to understand more clearly.

Hope u have the code which I gave in the last post.

So create a test program paste the code and try to debug.

Read only

0 Likes
1,109

Query :

"DATE1+4(4) EQ '0229'"

What does this syntax mean??

Read only

0 Likes
1,109

Date is of type DATS 8.

So if FEB 29 2006 will be as 20060229 in debugging u can see this.

So +() means offset of.

If date = '20060229'.

For taking year u have to specify date+0(4) which is equal to 2006.

For month and year date+4(4) which will return '0229'.

If you want month alone date+4(2).

If you want date alone date+6(2).

It starts with 0, so 0 -7 count.

sy-datum format is YYYYMMDD.

hope u r clear now.

Message was edited by:

Judith Jessie Selvi

Read only

0 Likes
1,109

In general DATE1+a(b) means the part of DATE, from b number of characters starting from 'a'th position.

if date1 = 20070229, then date1+4(4) will be 0229.

Regards,

Ravi

Read only

0 Likes
1,109

Hi,

As we know that date variable is of size 8 characters.

In SAP it is stored as YYYYMMDD. i.e. for example 2nd October, 2006 will be stored in SAP as 20061002.

Date1+4(4) means MMDD of YYYYMMDD.

Syntax is

Variable+offset(length).

In this case if you refer date1+0(4), you will get YYYY. i.e. starting from 0, next 4 digits in the variable.

I hope this will solve your purpose.

Regards,

Navin