12-19-2006 12:22 PM
Hi All,
I have one requirement.
I have two dates: Start Date and End Date.
If i subtract these two days , i will be getting the no of days between these two dates.
But for my logic , this subtraction should exclude the weekends.
For Example:
If the Start date is: 01.01.2006 and End Date is 08.01.2006, and if i subtract these two days, the difference is: 7 But this includes weekends also.
If we exclude the weekends i.e : 01.01.2006, 07.01.2006 and 08.01.2006 the difference would be 5.
How to go ahead with this logic.
Thanks in Advance.
Abhijith H
12-19-2006 2:23 PM
Hi Abhijith,
1. DATE_CHECK_WORKINGDAY
This is one useful FM
2. Try this code (just copy paste)
IT DOES EXACTLY WHAT U REQUIRE.
REPORT abc.
data : num type i.
parameters : frdate type sy-datum default '20051216'.
parameters : todate type sy-datum default '20051221'.
perform getinfo using frdate todate changing num.
break-point.
&----
*& Form getinfo
&----
text
----
FORM getinfo USING fromdate todate CHANGING numofdays type i.
DATA : d TYPE sy-datum.
d = fromdate - 1.
DO.
d = d + 1.
IF d > todate.
EXIT.
endif.
CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
EXPORTING
date = d
factory_calendar_id = '01'
message_type = 'I'
EXCEPTIONS
date_after_range = 1
date_before_range = 2
date_invalid = 3
date_no_workingday = 4
factory_calendar_not_found = 5
message_type_invalid = 6
OTHERS = 7.
IF sy-subrc = 0.
numofdays = numofdays + 1.
write 😕 d.
ENDIF.
ENDDO.
ENDFORM. "getinfo
I hope it helps.
Regards,
Amit M.
12-19-2006 12:23 PM
Hello Abjith,
Use this FM,
CALL FUNCTION 'RKE_SELECT_FACTDAYS_FOR_PERIOD'
EXPORTING
I_DATAB = DATAB
I_DATBI = DATBI
I_FACTID = '01'
TABLES
ETH_DATS = LT_RKE_DAT
EXCEPTIONS
DATE_CONVERSION_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
If useful reward.
Vasanth
12-19-2006 12:25 PM
12-19-2006 12:27 PM
https://forums.sdn.sap.com/click.jspa?searchID=464685&messageID=875585
check this link to calculate working days and the below one to calculate holidays
https://forums.sdn.sap.com/click.jspa?searchID=464719&messageID=2475094
<b>award points if found helpful</b>
12-19-2006 12:36 PM
hi ,
Are u looking for a country specifc working days if so what country is it ?
or at a plant level to fetch the working days on a calender..
confirm this..
regards,
vijay
12-19-2006 12:49 PM
Hi Abhijit,
Try this.
data: i_dates type standard table of RKE_DAT,
cnt type i.
CALL FUNCTION 'RKE_SELECT_FACTDAYS_FOR_PERIOD'
EXPORTING
i_datab = '20061201' "From date
i_datbi = sy-datum "To date
i_factid = 'GB' "Factory Calendar
tables
eth_dats = i_dates.
describe table i_dates lines cnt.
write cnt. "No. of working days for the period based on the Factory calendar.
Trust this helps.
Regards,
Biju
12-19-2006 1:00 PM
what if country is 'IN' / india or 'CN' china and if saturday is a working day then u cannot exclude that day right ..
see the application is for which country this has to be applicable and this is determined at plant level in table T001W-FABKL .
The calender is maintained IN TABLE TFACD where the public holidays and general holidays are maintained .
so when u want to fetch teh number of working days it must be clear that what is the calender id applicable .
You Can check the same in FM <b>DATE_CHECK_WORKINGDAY</b>
here is the code to demonstrate the working days.
REPORT ZEX8 .
tables : VBAK.
PARAMETERS : P_WERKS LIKE T001W-WERKS OBLIGATORY.
select-options : s_date for vbak-erdat OBLIGATORY.
DATA : WRK_DYS TYPE I ,
TOT_DYS TYPE I ,
HOL_DYS TYPE I.
DATA: calid LIKE tfacd-ident.
DATA : BEGIN OF ITAB OCCURS 0,
DATE LIKE SY-DATUM,
END OF ITAB.
DATA : BEGIN OF IWRK OCCURS 0,
WRDATE LIKE SY-DATUM,
END OF IWRK.
DATA : BEGIN OF IHOL OCCURS 0,
HOLDAY LIKE SY-DATUM,
END OF IHOL.
DATA : V_DATE LIKE SY-DATUM.
IF S_DATE-LOW ne '00000000'.
PERFORM FILLTAB.
ENDIF.
* LOOP AT ITAB.
* WRITE:/ ITAB-DATE.
* ENDLOOP.
DESCRIBE TABLE ITAB LINES TOT_DYS.
WRITE:/ 'TOTAL NUMBER OF DAYS ARE', TOT_DYS.
PERFORM FCT_CALID.
PERFORM WRK_DAYS.
PERFORM DISP_WRKDYS.
PERFORM DISP_HOLDAYS.
form FILLTAB.
ITAB-DATE = S_DATE-LOW.
APPEND ITAB.
IF S_DATE-HIGH ne '00000000'.
IF S_DATE-LOW = S_DATE-HIGH.
EXIT.
ENDIF.
ADD 1 To S_DATE-LOW.
PERFORM FILLTAB.
ENDIF.
endform. " FILLTAB
*&---------------------------------------------------------------------*
*& Form WRK_DAYS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form WRK_DAYS.
LOOP AT ITAB.
V_DATE = ITAB-DATE.
CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
EXPORTING
date = V_DATE
factory_calendar_id = CALID
message_type = 'I'
EXCEPTIONS
DATE_AFTER_RANGE = 1
DATE_BEFORE_RANGE = 2
DATE_INVALID = 3
DATE_NO_WORKINGDAY = 4
FACTORY_CALENDAR_NOT_FOUND = 5
MESSAGE_TYPE_INVALID = 6
OTHERS = 7
.
IF SY-SUBRC EQ 0.
IWRK-WRDATE = V_DATE.
APPEND IWRK.
CLEAR IWRK.
ELSE.
IHOL-HOLDAY = V_DATE.
APPEND IHOL.
CLEAR IHOL.
ENDIF.
ENDLOOP.
endform. " WRK_DAYS
*&---------------------------------------------------------------------*
*& Form DISP_WRKDYS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form DISP_WRKDYS.
DESCRIBE TABLE IWRK LINES WRK_DYS.
LOOP AT IWRK.
AT FIRST.
skip.
WRITE:/5 'WORKING DAYS'.
SKIP.
ENDAT.
WRITE:/5 IWRK-WRDATE.
ENDLOOP.
WRITE:/ 'Total number of working days are', wrk_Dys.
skip.
endform. " DISP_WRKDYS
*&---------------------------------------------------------------------*
*& Form DISP_HOLDAYS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form DISP_HOLDAYS.
DESCRIBE TABLE IHOL LINES HOL_DYS.
LOOP AT IHOL.
AT FIRST.
skip.
WRITE:/5 'NON WORKING DAYS ARE '.
SKIP.
ENDAT.
WRITE:/5 IHOL-HOLDAY.
ENDLOOP.
WRITE:/ 'Total number of NON working days are', HOL_Dys.
skip.
endform. " DISP_HOLDAYS
*&---------------------------------------------------------------------*
*& Form FCT_CALID
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form FCT_CALID.
SELECT SINGLE fabkl INTO calid FROM t001w
WHERE werks = P_werks.
endform. " FCT_CALID
regards,
vijay.
12-19-2006 2:23 PM
Hi Abhijith,
1. DATE_CHECK_WORKINGDAY
This is one useful FM
2. Try this code (just copy paste)
IT DOES EXACTLY WHAT U REQUIRE.
REPORT abc.
data : num type i.
parameters : frdate type sy-datum default '20051216'.
parameters : todate type sy-datum default '20051221'.
perform getinfo using frdate todate changing num.
break-point.
&----
*& Form getinfo
&----
text
----
FORM getinfo USING fromdate todate CHANGING numofdays type i.
DATA : d TYPE sy-datum.
d = fromdate - 1.
DO.
d = d + 1.
IF d > todate.
EXIT.
endif.
CALL FUNCTION 'DATE_CHECK_WORKINGDAY'
EXPORTING
date = d
factory_calendar_id = '01'
message_type = 'I'
EXCEPTIONS
date_after_range = 1
date_before_range = 2
date_invalid = 3
date_no_workingday = 4
factory_calendar_not_found = 5
message_type_invalid = 6
OTHERS = 7.
IF sy-subrc = 0.
numofdays = numofdays + 1.
write 😕 d.
ENDIF.
ENDDO.
ENDFORM. "getinfo
I hope it helps.
Regards,
Amit M.
12-19-2006 2:27 PM
FORM calculate_difference USING p_d1
p_d2
p_diff.
DATA: a(1) TYPE c,
data1 like sy-datum,
data2 like sy-datum.
p_diff = 0.
data1 = p_d1.
data2 = p_d2.
IF data1 < data2.
p_diff = -1.
ELSE.
WHILE data2 < data1.
CLEAR a.
CALL FUNCTION 'DATE_CONVERT_TO_FACTORYDATE'
EXPORTING
date = data2
factory_calendar_id = 'IT'
IMPORTING
workingday_indicator = a.
IF a IS INITIAL.
p_diff = p_diff + 1.
ENDIF.
data2 = data2 + 1.
ENDWHILE.
ENDIF.
ENDFORM.