Working Days Between Dates Using HR Calendar

How to find No. of working days between 2 dates using HR CALENDAR?

Answer:

Use the below code.

DATA: v_week1 LIKE scal-week,

v_week2 LIKE scal-week.

DATA: v_work_days like MDCAL-ANZFD,

v_total_work_days like MDCAL-ANZFD,

v_wotnr type p.
 

CALL FUNCTION 'DATE_GET_WEEK'

EXPORTING

date = '20110321'

IMPORTING

week = v_week1

EXCEPTIONS

date_invalid = 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.
 
 

CALL FUNCTION 'DATE_GET_WEEK'

EXPORTING

date = '20111129'

IMPORTING

week = v_week2

EXCEPTIONS

date_invalid = 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.
 

clear v_wotnr.
 

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

datum = '20110321'

IMPORTING

WOTNR = v_wotnr.
 

if v_wotnr le 5.

v_total_work_days = v_total_work_days + ( 5 - v_wotnr ) + 1.

endif.
 

clear v_wotnr.
 

CALL FUNCTION 'DAY_IN_WEEK'

EXPORTING

datum = '20111129'

IMPORTING

WOTNR = v_wotnr.
 

if v_wotnr le 5.

v_total_work_days = v_total_work_days + v_wotnr.

endif.
 

v_week1 = v_week1 + 1.

v_week2 = v_week2 - 1.
 

while v_week1 le v_week2.
 

clear v_work_days.
 

CALL FUNCTION 'WEEK_GET_NR_OF_WORKDAYS'

EXPORTING

WEEK = v_week1

* DAY = DAY

fabkl = 'US' "Give Thailand ID here

* REST_OF_WEEK = REST_OF_WEEK

IMPORTING

NR_OF_WORKDAYS = v_work_days

EXCEPTIONS

WEEK_INVALID = 1

DAY_INVALID = 2

FABKL_INVALID = 3

FABKL_EMPTY = 4

OTHERS = 5

.

IF sy-subrc 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

v_total_work_days = v_total_work_days + v_work_days.

v_week1 = v_week1 + 1.

endwhile.

write:/ 'Number of working days = ', v_total_work_days. 

ABAP Tips

See Also
HR Report To See Description Per Qualification

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.