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: 

CHALLANGE : EXPLAIN THIS CODE!!!

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Regards,

John.

former_member181962
Active Contributor
0 Kudos

Hi tejas,

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

Regards,

Ravi

Former Member
0 Kudos

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.

0 Kudos

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!!

0 Kudos

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.

0 Kudos

Query :

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

What does this syntax mean??

0 Kudos

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

0 Kudos

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

0 Kudos

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