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: 

How to subtract 1 from a date field???

Former Member
0 Kudos

Howdy,

I want to get the previous day to the one that is entered by the user. Thereofre I coded the following:

Code:

<b>DATA: lv_date TYPE date,

new_datab TYPE knvh-datab.

lv_date = new_datab.

lv_date = lv_date - 1. </b>

lv_date should now have the previous day to that entered in new_date.

However I ran it for today 01.03.2005 and it does not give me 28.02.2005?

Instead it gives me 00.03.2005????

Does anyone know why this would be occurring?

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Make sure that your fields are "date" fields. Type sy-datum or type d.



parameters: p_datum type sy-datum.

p_datum = p_datum - 1.

Regards,

Rich Heilman

5 REPLIES 5

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Make sure that your fields are "date" fields. Type sy-datum or type d.



parameters: p_datum type sy-datum.

p_datum = p_datum - 1.

Regards,

Rich Heilman

0 Kudos

In other words, change this line from



DATA: lv_date TYPE date,

to



DATA: lv_date TYPE sy-datum,

Regards,

Rich Heilman

christian_wohlfahrt
Active Contributor
0 Kudos

Hi Steve,

you should have value 20050301 in new_datab (internal format!).

Then

SUBTRACT 1 FROM lv_date.

will work (at least date adding / subtracting is handled internally correct).

Regards,

Christian

Former Member
0 Kudos

You should be a doctor Rich! You've cured my headache

Do you know why type <b>date</b> does not work?

Thanks again!

0 Kudos

I would say because that data element "date" uses the domain CHAR08 which is a CHAR data type. On the other hand, data element "sydatum" uses the domain SYDATS which is a DATS data type.

Regards,

Rich Heilman