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: 

Days in week/month

Former Member
0 Kudos
297

Hi,

I need to calculate the number of days in a week in a month. Let me give an example:

Week |-----|---|---|---|-----|

Month -|


|----

Result.2....5......7......7........7....3....4

How is that done?

Thanks

Lars

1 ACCEPTED SOLUTION

Former Member
0 Kudos
117

Chk this out

REPORT ZSN_DATETEST .

data: date like sy-datum .

data: char(8).

data: weekday type p.

data: days type p.

data: result type p.

data: temp type p.

*-- Get the first date of the month

date = '20060201'.

char = date.

char+6(2) = '01'.

date = char.

*-- Get the week of the first date of the month

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

DATUM = date

IMPORTING

WOTNR = weekday

.

write:/ 'Weekday', weekday.

*-- Get the no of days in a month

CALL FUNCTION 'HR_E_NUM_OF_DAYS_OF_MONTH'

EXPORTING

P_FECHA = date

IMPORTING

NUMBER_OF_DAYS = days

.

write:/ 'No of days', days.

*-- No of days in a week in a month

result = 7 - weekday.

temp = result.

write:/ result.

do.

temp = temp + 7.

if temp < days.

result = 7.

write:/ result.

else.

temp = temp - 7.

result = days - temp..

write:/ result.

exit.

endif.

enddo.

6 REPLIES 6

Former Member
0 Kudos
117

hi,

We are not able to understand the requirement clearly.

Could you explain that will a real scenario.

Regards.

Anjali

0 Kudos
117

I need it in a BW start routine where I’m merging two different data sources. One of the data sources has the data in weekly buckets the other has it in daily… The output should be in daily buckets.

So what I’m trying to do is divide the weekly data in to the days in the week. The problem is that months are a part of the key for the weekly bucket…

So for week 5 I’ll get two records:

Week Month Data

200605 012006 50

200605 022006 250

The first record has 2 days in week 5 the second has 5. And that is what I would like to calculate…

0 Kudos
117

Hi Lars,

I am still not very clear with ur reqmt.

What i understand is you are looking for a no. of days in a week which can be achieved by using FM :Day in week.

<b>Sample code for the same:</b>

DATA: BEGIN OF DAYNAMES OCCURS 0.

INCLUDE STRUCTURE T246.

DATA: END OF DAYNAMES.

DATA: V_WEEK LIKE SCAL-WEEK,

WOTNR TYPE P,

V_MONDAY LIKE SCAL-DATE.

  • GET THE CURRENT WEEK OF A DATE

CALL FUNCTION 'DATE_GET_WEEK'

EXPORTING

DATE = SY-DATUM

IMPORTING

WEEK = V_WEEK "YYYYWW

EXCEPTIONS

DATE_INVALID = 1

OTHERS = 2.

  • GET THE DATE OF THE FIRST MONDAY OF THE WEEK

CALL FUNCTION 'WEEK_GET_FIRST_DAY'

EXPORTING

WEEK = V_WEEK

IMPORTING

DATE = V_MONDAY

EXCEPTIONS

WEEK_INVALID = 1

OTHERS = 2.

  • DAY NUMBER OF A DATE

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

DATUM = SY-DATUM

IMPORTING

WOTNR = WOTNR.

  • NAMES OF THE DAYS

CALL FUNCTION 'WEEKDAY_GET'

EXPORTING

LANGUAGE = SY-LANGU

TABLES

WEEKDAY = DAYNAMES.

READ TABLE DAYNAMES WITH KEY WOTNR = WOTNR.

WRITE:/ 'DATE:', SY-DATUM,

/ 'WEEK NUMBER:', V_WEEK+4,

/ 'FIRST DAY OF WEEK:', V_MONDAY,

/ 'CURRENT DAY OF WEEK:', DAYNAMES-LANGT.

Few more functional module which may also help your reqmts. , juct check them out.

WEEK_GET_NR_OF_WORKDAYS

WEEK_GET_FIRST_DAY

WEEKDAY_GET

MONTH_NAMES_GET

LAST_DAY_IN_PERIOD_GET

FIMA_DAYS_AND_MONTHS_AND_YEARS

DAYS_BETWEEN_TWO_DATES

Hope this will help you.

Cheers

Sunny

Rewrd points, if found helpful

Former Member
0 Kudos
117

Hai Lars,

Your Question is not clear.What i understood is...

When you give input as Date and in outupt it should give you two things.

1) Month of the Date

2) all Weeks of the month (starting date and Ending Date)

Right...

1. MONTH_NAMES_GET

This is the FM

2. try this code ( just copy paste)

REPORT abc.

DATA : mname(10) TYPE c.

PARAMETER : d TYPE sy-datum.

PERFORM getmonth USING d mname.

WRITE 😕 mname.

&----


*& Form getmonth

&----


  • text

----


  • -->M text

  • -->MNAME text

----


FORM getmonth USING d mname.

DATA : month_names LIKE t247 OCCURS 0 WITH HEADER LINE.

DATA : m(2) TYPE c.

m = d+4(2).

CALL FUNCTION 'MONTH_NAMES_GET'

  • EXPORTING

  • LANGUAGE = SY-LANGU

  • IMPORTING

  • RETURN_CODE =

TABLES

month_names = month_names

EXCEPTIONS

month_names_not_found = 1

OTHERS = 2

.

READ TABLE month_names INDEX m.

IF sy-subrc = 0.

mname = month_names-ltx.

ENDIF.

ENDFORM. "getmonth

Regards,

Srikanth.

Please reward points if helpful.

Thanks in advance.

Former Member
0 Kudos
118

Chk this out

REPORT ZSN_DATETEST .

data: date like sy-datum .

data: char(8).

data: weekday type p.

data: days type p.

data: result type p.

data: temp type p.

*-- Get the first date of the month

date = '20060201'.

char = date.

char+6(2) = '01'.

date = char.

*-- Get the week of the first date of the month

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

DATUM = date

IMPORTING

WOTNR = weekday

.

write:/ 'Weekday', weekday.

*-- Get the no of days in a month

CALL FUNCTION 'HR_E_NUM_OF_DAYS_OF_MONTH'

EXPORTING

P_FECHA = date

IMPORTING

NUMBER_OF_DAYS = days

.

write:/ 'No of days', days.

*-- No of days in a week in a month

result = 7 - weekday.

temp = result.

write:/ result.

do.

temp = temp + 7.

if temp < days.

result = 7.

write:/ result.

else.

temp = temp - 7.

result = days - temp..

write:/ result.

exit.

endif.

enddo.

0 Kudos
117

Excellent… With a few adjustments it works just as it should…


*&---------------------------------------------------------------------*
*& Report  ZZLBHJ02                                                    *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ZZLBHJ02                                .
data: date like sy-datum .
data: date2 like sy-datum .
data: n_dayinweek  like SCAL-INDICATOR.
data: char(8).
data: weekday type p.
data: days type p.
data: result type p.
data: temp type p.

*-- Get the first date of the month
date = '20060201'.
char = date.
char+6(2) = '01'.
date = char.
break lbhj.
*-- Get the week of the first date of the month
 CALL FUNCTION 'DATE_COMPUTE_DAY'
        EXPORTING
          DATE = date
        IMPORTING
          DAY  = n_dayinweek.

write:/ 'Weekday', n_dayinweek.

*-- Get the no of days in a month
      CALL FUNCTION 'SLS_MISC_GET_LAST_DAY_OF_MONTH'
        EXPORTING
          DAY_IN            = date
        IMPORTING
          LAST_DAY_OF_MONTH = date2.
          days = date2+6(2).
write:/ 'No of days', days.

*-- No of days in a week in a month
result = 7 - n_dayinweek.
result = result + 1.
temp = result.
write:/ result.
do.
  temp = temp + 7.
  if temp < days.
    result = 7.
    write:/ result.
  else.
    temp = temp - 7.
    result = days - temp.
    write:/ result.
    exit.
  endif.
enddo.