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

date logic

Former Member
0 Likes
624

Hi All,

The following code gives the output of the first monday of a month. Can any one help me in getting the first monday of the last month.

DATA: G_DATE0 LIKE SY-DATUM.

DATA: G_DATE1 LIKE SY-DATUM.

DATA: G_WOTNR TYPE P.

G_DATE1 = G_DATE0.

DATE1+6(2) = '01'.

DO.

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

DATUM = G_DATE1

IMPORTING

WOTNR = WOTNR.

if g_wotnr = 1.

exit.

endif.

ADD 1 TO G_DATE1.

ENDDO.

Points will be awarded for sure.

Regards,

Srik.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
536

Hi,

see this code.


DATA: G_DATE0 LIKE SY-DATUM.
DATA: G_DATE1 LIKE SY-DATUM.
DATA: G_WOTNR TYPE P.

g_date0 = sy-datum.
G_DATE1 = G_DATE0.
g_DATE1+4(2) = g_DATE1+4(2) - 1.
g_DATE1+6(2) = '01'.

DO.

CALL FUNCTION 'DAY_IN_WEEK'
EXPORTING
DATUM = G_DATE1
IMPORTING
WOTNR = g_WOTNR.

if g_wotnr = 1.
exit.
endif.
ADD 1 TO G_DATE1.

ENDDO.

write:/ g_date1.

rgds,

bharat.

4 REPLIES 4
Read only

Former Member
0 Likes
537

Hi,

see this code.


DATA: G_DATE0 LIKE SY-DATUM.
DATA: G_DATE1 LIKE SY-DATUM.
DATA: G_WOTNR TYPE P.

g_date0 = sy-datum.
G_DATE1 = G_DATE0.
g_DATE1+4(2) = g_DATE1+4(2) - 1.
g_DATE1+6(2) = '01'.

DO.

CALL FUNCTION 'DAY_IN_WEEK'
EXPORTING
DATUM = G_DATE1
IMPORTING
WOTNR = g_WOTNR.

if g_wotnr = 1.
exit.
endif.
ADD 1 TO G_DATE1.

ENDDO.

write:/ g_date1.

rgds,

bharat.

Read only

0 Likes
536

if you use the above code, ensure you modify it as such


g_date0 = sy-datum.
G_DATE1 = G_DATE0.
g_DATE1+4(2) = g_DATE1+4(2) - 1.

if g_DATE1+4(2) = '00'.
  g_DATE1+4(2) = '12'.
  g_DATE1+0(4) = g_DATE1+0(4) - 1.
endif.

g_DATE1+6(2) = '01'.

There is a Function that will compute the date with adjustments to Month or Year. It might be better to look for that FM. I can't recall the name of hand.

Read only

0 Likes
536

if actual month is 01 above code will not work. you have to use this syntax.


if G_DATE1+4(2) eq '01'.
    move '12' to G_DATE1+4(2).
    subtract 1 from G_DATE1+0(4) .
else.
    subtract 1 from G_DATE1+4(2) .
endif.
    move '01' to G_DATE1+6(2).

ibrahim

Read only

andreas_mann3
Active Contributor
0 Likes
536

try:

PARAMETERS g_date0 LIKE sy-datum.

DATA: g_date1 LIKE sy-datum.

DATA: g_wotnr TYPE p.

INITIALIZATION.

CONCATENATE sy-datum(4) '1201' INTO g_date0.

START-OF-SELECTION.

g_date1 = g_date0.

DO.

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

datum = g_date1

IMPORTING

wotnr = g_wotnr.

IF g_wotnr = 1.

EXIT.

ENDIF.

ADD 1 TO g_date1.

ENDDO.

WRITE : g_date0, / g_date1, g_wotnr.

hope that helps

Andreas