cancel
Showing results for 
Search instead for 
Did you mean: 

Numerical manipulation with system date SY-DATUM

Former Member
0 Kudos

Hi all,

I have the requirement wherein I need to do numerical manipulation on year, month, and date of SY-DATUM. For that, I extracted the date, month, and year parts from SY-DATUM as below.

data: year(4) type c,

month(2) type c,

date(2) type c.

year = sy-datum(4).

month = sy-datum+4(2).

date = sy-datum+6(2).

write: / year , " 2005

/ month , " 07

/ date. " 19

How do I add numerical values to above year, month and date values? As they are in character format, I could not add numerical values directly.

Thanks in advance.

Cheers,

Vicky.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi, you can reference the following link, I think your question is similar

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Vicky,

Firstly, even if your variables for year, month and date are in character format, you can do simple arithmetic operations like addition and subtraction on them. There's an automatic type conversion that happens internally for such operations.

Secondly, instead of declaring your variables to be of type <b>c</b>, you can declare them to be of type <b>n</b>. That will make your code better in the sense that you're now doing the addition on <i>Numbers</i>, which is clearly possible without any problem.

But if your logic is more complicated, then you can use the function modules that others have suggested.

Regards,

Anand Mandalika.

Former Member
0 Kudos

HI all,

thank you so much for your helpful answers. I already awarded points to you all.

Cheers,

Vicky.

Former Member
0 Kudos

Well, the easiest thing to do is to simply add a number (of days) to a date.


data: w_date like sy-datum.
w_date = sy-datum.
w_date = w_date + 90.
* w_date has a date 90 days in the future.

But if you have to add months or years, you can use FM RP_CALC_DATE_IN_INTERVAL:


data: w_date like sy-datum.
data: new_date like sy-datum.
CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL' 
     EXPORTING                           
          DATE      = w_date   
          DAYS      = 0                  
          MONTHS    = 3                  
          SIGNUM    = '+'                
          YEARS     = 0                  
     IMPORTING                           
          CALC_DATE = new_date   
     EXCEPTIONS                          
          OTHERS    = 1.                 
* new_date has a value 3 months in the future.

You can use combinations of days, months and years.

Rob

Also bear in mind that unlike ordinary arithmetic, the order of operation is important in date arithmetic. Adding a certain number of days to a date and then some months doesn't always give the same answer if you do it the other way around:

2005/02/28 + 1 day + 3 months = 2005/06/01

2005/02/28 + 3 months + 1 day = 2005/05/29

Message was edited by: Rob Burbank

Former Member
0 Kudos

Hi Rob and all,

I used the function module 'RP_CALC_DATE_IN_INTERVAL'. But when I executed, it gave me run-time error by saying that the FM is not in the function library. How do I make this FM available in the function library?

Cheers,

Vicky.

Former Member
0 Kudos

That's odd. It's definitely in our 4.6C system and I'm sure it was there in 3.1H. What version are you on?

Somewhere in this or another thread, it was suggested that there are a number of FMs for date calculation. Maybe one of them would suit your purpose.

Rob

OK - I checked our 6.40 system and it's not there; it looks as though SAP removed the function group.

The following should work:


REPORT ztest.

PARAMETERS:
             date LIKE p0001-begda,
             days LIKE t5a4a-dlydy,
             months LIKE t5a4a-dlymo,
             signum, "LIKE t5a4a-split DEFAULT '+',
             years LIKE t5a4a-dlyyr.


DATA: act_date   TYPE d,
      dys        TYPE p,
      mon        TYPE p,
      yrs        TYPE p,
      calc_date  TYPE d,
      ttl_yrs    TYPE p
                 VALUE 0,
      round      TYPE p
                 DECIMALS 2
                 VALUE '0.50'.
rp-lowdate-highdate.

act_date = date.
IF ( date <> high-date OR
     signum = '-' ) AND
   ( date <> low-date OR
     signum <> '-' ).
  IF signum = '-'.
    dys = - days.
    mon = - months.
    yrs = - years.
    round = - round.
  ELSE.
    dys = days.
    mon = months.
    yrs = years.
  ENDIF.

  act_date = act_date + dys.
  IF mon <> 0.
    PERFORM adjust_months USING act_date+4(2)
                                mon ttl_yrs round.
    UNPACK mon TO act_date+4(2).
  ENDIF.

  ttl_yrs = yrs + ttl_yrs.
  act_date(4) = act_date(4) + ttl_yrs.

  PERFORM valid_date USING act_date
                           signum.
ENDIF.

calc_date = act_date.

WRITE: /001 calc_date.

FORM adjust_months USING act_month mon yrs round.

  IF mon > 11 OR
     mon < -11.
    yrs = mon / 12 - round.
    mon = mon - yrs * 12.
  ENDIF.

  mon = act_month + mon.

  IF mon <= 0.
    yrs = yrs - 1.
    mon = mon + 12.
  ELSE.
    IF mon > 12.
      yrs = yrs + 1.
      mon = mon - 12.
    ENDIF.
  ENDIF.
ENDFORM.

FORM valid_date USING act_date sign.

  DATA: test_date  TYPE d,
        corr_date  TYPE d.

  corr_date = act_date.

  DO.
    test_date = 1 + corr_date - 1.

    IF test_date = corr_date.
      EXIT.
    ENDIF.

    IF sign = '-'.
      corr_date+6(2) = corr_date+6(2) - 1.
    ELSE.
      corr_date+6(2) = '01'.
      corr_date = corr_date + 32.
      corr_date+6(2) = '01'.
    ENDIF.
  ENDDO.

  act_date = corr_date.

Message was edited by: Rob Burbank

Former Member
0 Kudos

Hi Rob,

thank you for the info. Unfortunately I could not run the program you supplied here. My system is complaining about the global fields likr: p0001, t5a4a - which are there in your code. So, it could not help me much.

Any way, I solved my problem using other suggestions of this thread.

Cheers,

Vicky.