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

Why mod value is showing incorrect

Former Member
0 Likes
2,407

In function module DATE_COMPUTE_DAY the value of the DAY_P is showing incorrect.

I mean,for,

DAY_P = DATE MOD 7.

if date is 2011/08/24 the value is showing 4 for DAY_P. But mathematically the value is 6.

Or if we consider only date value as 24, mod value has to be 3 but its showing 4.

So please help.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,072

HI

It is showing correctly as 3.

please have a look below.

Import parameters Value

DATE 24.08.2011

Export parameters Value

DAY 3

Shiva

In function module DATE_COMPUTE_DAY the value of the DAY_P is showing incorrect.

I mean,for,

DAY_P = DATE MOD 7.

if date is 2011/08/24 the value is showing 4 for DAY_P. But mathematically the value is 6.

Or if we consider only date value as 24, mod value has to be 3 but its showing 4.

So please help.

13 REPLIES 13
Read only

Former Member
0 Likes
2,073

HI

It is showing correctly as 3.

please have a look below.

Import parameters Value

DATE 24.08.2011

Export parameters Value

DAY 3

Shiva

Read only

0 Likes
2,072

The output of the function module is correct but for the line DAY_P = Date MOD 7 the value is different. How is the mod function is used in this scenario.

Read only

0 Likes
2,072

It looks like the MOD operator behaves differently when using a type Datum operator. If you make the same operation using a type I for instance the result is the one you say: 6

Let's say that the MOD operator takes the number of days of the date into account, in this case it would be 734374 days, and if you 734374 MOD 7 then you get your 4.

So, bottom line: the operation is not 20110824 MOD 7, it actually is 734374 MOD 7

Read only

0 Likes
2,072

When you translate the standard text, it says

01/01/1900 was a Saturday. All days from 01.01.1900 from, are numbered from ABAP. 01.01.1900 has the number 0, the 02.01.1900 the number 1, etc.. Of 01.01.1900 was

Saturday.

DAY_P contains 0 for Saturday, 1 for Sunday, etc.. and must

the calendar notation: 1 for Monday, 2 are adapted for Tuesday, etc.,.

Since today is Wednesday, it is giving as 3.

Shiva

Read only

0 Likes
2,072

Hi, Jose is completely right.

@Shiva, a little correction :

01.01.1900 has the number 0, the 02.01.1900 the number 1, etc.

Actually, the counting starts from 01.01.0001 , which is day 0.

DAY_P contains 0 for Saturday, 1 for Sunday, etc..

Yes, this is for DATE MOD 7, if the result is 0, then it's a Saturday, 1 is for Sunday, ...

and must the calendar notation: 1 for Monday, 2 are adapted for Tuesday, etc.

Since today is Wednesday, it is giving as 3.

You obtain it only if you use DATE_COMPUTE_DAY for instance. It's not related to MOD. Note: if we use SY-FDAYW, then it's 0 for Sunday up to 6 for Saturday...

Sandra

Read only

0 Likes
2,072

@Sandra, I didn't do much R&D on the same. I just translated the same from FM and pasted.Thanks for your clarification.

Shiva

Read only

0 Likes
2,072

Jose,

Can you please let me know how you arrived at the figure 734374 for 24.08.2011.

Thanks,

K.Kiran.

Read only

0 Likes
2,072

data: date type dats value '20110824'.
data: int    type i.

int = date.
write int.
Read only

0 Likes
2,072

Jose,

Yes,I am seeing when dats value is getting moved to i it is getting changed.

Can you please explain why type dats value when moved to type i gets changed to some other value.

Thanks.

K.Kiran.

Read only

0 Likes
2,072

see chapter "elementary data type" -> "source data type : D" at this location : [ABAP documentation: Conversion rules|http://help.sap.com/abapdocu_70/en/ABENCONVERSION_RULES.htm ]

Read only

0 Likes
2,072

Hi,

The value obtained after type conversion from D to I gives the result. That is fine. But if we mod only the date value for example 26 MOD 7 the result is 5 which indicates friday. But according to above code its give 6 which is 1 greater. So can you please clarify how we are getting 1 why not the exact value.

Read only

0 Likes
2,072

Hi,

26 MOD 7 the result is 5 which indicates friday. But according to above code its give 6 which is 1 greater. So can you please clarify how we are getting 1 why not the exact value.

No. 5 is Thursday. 26 corresponds to date 25.01.0001.

First date of Julian/Gregorian calendar is 01.01.0001, it was a Saturday, when converted to integer in ABAP becomes 0. 02.01.0001 is a Sunday and is day number 1. Etc.

Try this code, you'll understand.


PARAMETERS p_date TYPE d DEFAULT sy-datum.
DATA i TYPE i.
i = p_date MOD 7.
WRITE : / 'Date', p_date, 'is a'.
CASE i.
  WHEN 0. WRITE 'Saturday'.
  WHEN 1. WRITE 'Sunday'.
  WHEN 2. WRITE 'Monday'.
  WHEN 3. WRITE 'Tuesday'.
  WHEN 4. WRITE 'Wednesday'.
  WHEN 5. WRITE 'Thursday'.
  WHEN 6. WRITE 'Friday'.
ENDCASE.

Sandra

Read only

0 Likes
2,072

Hi

In according what Sandra says it can add the last step:

DATA I_WEEK TYPE I.
*----------------------------------------------------------------------*
* The first day of the week is Monday so:
* Monday     is 1
* Tuesday    is 2
* Wednesday  is 3
* Thursday   is 4
* Friday     is 5
* Saturday   is 6
* Sunday     is 7
*----------------------------------------------------------------------*

CASE I.
  WHEN 0. I_WEEK = 6. "Saturday'.
  WHEN 1. I_WEEK = 7. "Sunday'.
  WHEN 2. I_WEEK = 1. "Monday'.
  WHEN 3. I_WEEK = 2. "Tuesday'.
  WHEN 4. I_WEEK = 3. "Wednesday'.
  WHEN 5. I_WEEK = 4. "Thursday'.
  WHEN 6. I_WEEK = 5. "Friday'.
ENDCASE.

* So the rule is:

IF I > 1.
  I_WEEK = I - 1.
ELSE.
  I_WEEK = I + 6.
ENDIF.

Max