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

Calculate working days between two dates

Former Member
0 Kudos
9,025

Hi,

Is there any function available to calculate the number of working days between two dates based on the factory calendar. I know there is a function to check if a particular day is working or not.

Regards

Rani

13 REPLIES 13
Read only

suresh_datti
Active Contributor
0 Kudos
5,182

Hi Rani,

I don't think there is one.. but you can put the dates in a loop , call FVD_MD_TOOLS_CHECK_WORKDAY and increment a counter for each workday to obtain the number of work days.

Regards,

Suresh Datti

Read only

0 Kudos
5,182

Thanks..i could think of doing it that way...was on the look out for a nicer way

Read only

0 Kudos
5,182

Unfortunately there is no single function module that gives you this information. Use HOLIDAY_GET function module by passing your two dates and the factory calendar. Count the number of rows returned in the holiday internal table. Calculate the number of days between the two dates by simply subtracting from_date from to_date. Now the difference between the total number of days minus the number of holidays is your total number of work days. This is easier to do than looping.

Srinivas

Read only

0 Kudos
4,376

Hello, the problem with this is that it will include weekends and bank holidays, which are not working days. We need to count the number of weekdays between two dates. 

Read only

Former Member
0 Kudos
5,182

Hi,

i think you need to create your FM to do that, or you need to call the FM in side the loop of a date table and capture the data.

regards

vijay

Read only

0 Kudos
5,182

Hi,

Check this FM and kindly reward points by clicking the star on the left of reply,if it helps.

Use the function module

WDKAL_DATE_ADD_FKDAYS

to get the No of working days between the date range.

and the function module,

WEEK_GET_NR_OF_WORKDAYS

gives the no of workable days in a week.

Read only

Former Member
0 Kudos
5,182

Hi Rani,

1. Independent PERFORM

for this purpose.

2. Using FM and logic

below is a FORM

which independelty

gives the NUMBER OF WORKING DAys

(inputs are : fromdate, todate, days)

eg. From 24-jan-2006

25-jan-2006

26-jan-2006 (republic day in india)

27-jan-2006

It will Return 3

3. see this code (just copy paste)

REPORT abc.

*----


DATA : days TYPE i.

DATA : dt TYPE sy-datum.

*----


SELECT-OPTIONS : mydate FOR sy-datum DEFAULT '20060124' TO '20060127'.

*----


START-OF-SELECTION.

PERFORM calcdays USING mydate-low mydate-high days.

WRITE days .

*----


  • FORM

*----


FORM calcdays USING fromdate todate days.

DATA : dt TYPE sy-datum.

dt = fromdate.

DO.

IF dt > todate.

EXIT.

ENDIF.

CALL FUNCTION 'DATE_CHECK_WORKINGDAY'

EXPORTING

date = dt

factory_calendar_id = 'IN'

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.

dt = dt + 1.

IF sy-subrc = 0.

days = days + 1.

ENDIF.

ENDDO.

ENDFORM. "calcdays

regards,

amit m.

Read only

Former Member
0 Kudos
5,182

hi,

Check out this code.

it_holidays gives the no. of holidays between the a given period.

x_date will give the no. of working days.

REPORT ZTEST NO STANDARD PAGE HEADING LINE-COUNT 65

LINE-SIZE 132

MESSAGE-ID ZZ.

PARAMETER : P_DATE LIKE SY-DATUM.

data: it_holidays like iscal_day occurs 0 with header line.

DATA: T_DATE LIKE SY-DATUM.

data : x_date(4) type c.

data: cnt type i.

REFRESH : IT_HOLIDAYS.

CLEAR : IT_HOLIDAYS.

T_DATE = SY-DATUM.

CALL FUNCTION 'HOLIDAY_GET'

EXPORTING

HOLIDAY_CALENDAR = 'US'

  • FACTORY_CALENDAR = ' '

DATE_FROM = P_DATE

DATE_TO = T_DATE

  • IMPORTING

  • YEAR_OF_VALID_FROM =

  • YEAR_OF_VALID_TO =

  • RETURNCODE =

TABLES

HOLIDAYS = IT_HOLIDAYS

EXCEPTIONS

FACTORY_CALENDAR_NOT_FOUND = 1

HOLIDAY_CALENDAR_NOT_FOUND = 2

DATE_HAS_INVALID_FORMAT = 3

DATE_INCONSISTENCY = 4

OTHERS = 5.

cnt = 0.

loop at it_holidays.

write 😕 it_holidays.

cnt = cnt + 1.

endloop.

x_date = t_date - p_date - cnt.

write 😕 x_date.

Regards,

Sailaja.

Read only

Former Member
0 Kudos
5,182

Try the function below.... It would give you all the dates between DATE_FROM and DATE_TO. Also, it would mark all the Sundays and Holidays with a FLAG = X based on the Calender ID input. Just count in the table for flag to get holidays or Unflagged to get working days. Remember to give the correct Calender ID

CALL FUNCTION 'DAY_ATTRIBUTES_GET'

EXPORTING

FACTORY_CALENDAR = 'IN'

  • HOLIDAY_CALENDAR = ' '

DATE_FROM = l_date_ini

DATE_TO = l_date_fin

LANGUAGE = SY-LANGU

NON_ISO = ' '

  • IMPORTING

  • YEAR_OF_VALID_FROM =

  • YEAR_OF_VALID_TO =

  • RETURNCODE =

TABLES

DAY_ATTRIBUTES = itab_attr

EXCEPTIONS

FACTORY_CALENDAR_NOT_FOUND = 1

HOLIDAY_CALENDAR_NOT_FOUND = 2

DATE_HAS_INVALID_FORMAT = 3

DATE_INCONSISTENCY = 4

OTHERS = 5

Read only

Former Member
0 Kudos
5,182

This is the FM

WDKAL_DATE_ADD_FKDAYS

Read only

Former Member
0 Kudos
5,182

Hi,

Check the below FM,

WDKAL_CALC_CONS_WORKDAYS_PER

WDKAL_DATE_ADD_FKDAYS

hope it helps.

Regards,

Anki Reddy

Read only

Former Member
0 Kudos
5,182

Hi Rani

Please use the below mentioned code snippet:

  • Variables to be used

DATA: wf_year(4) TYPE n,

wf_month(2) TYPE n,

wf_end_month(2) TYPE n,

wf_start_day(2) TYPE n,

wf_end_day(2) TYPE n,

wf_check_day(1) TYPE n,

wf_check_month(31) TYPE c,

wf_check_year(4) TYPE n,

wf_cnt TYPE i,

wf_first(1) TYPE c,

wf_year_ind(1) TYPE c,

wf_month_ind(1) TYPE c,

lv_str TYPE i,

lv_month TYPE i,

wf_exit.

  • Check that both dates are specified

IF start_date = 0 OR end_date = 0.

RAISE invalid_date.

ENDIF.

  • Assign initial values

wf_first = 'Y'.

wf_year = start_date+0(4).

wf_month = start_date+4(2).

wf_start_day = start_date+6(2).

wf_end_day = end_date+6(2).

  • Get data for requested year(s)

WHILE wf_year LE end_date+0(4).

wf_check_year = wf_year.

IF wf_year = end_date+0(4).

wf_year_ind = 'Y'.

ELSE.

wf_year_ind = 'N'.

ENDIF.

SELECT SINGLE * FROM tfacs

WHERE ident = calendar

AND jahr = wf_check_year.

IF sy-subrc NE 0.

RAISE no_calendar_for_year.

ENDIF.

  • Check which month

CLEAR wf_check_month.

CLEAR: wf_check_day.

IF wf_first = 'Y'.

wf_first = 'N'.

wf_cnt = start_date+6(2). " - 1.

ELSE.

CLEAR wf_cnt.

ENDIF.

MOVE wf_month TO lv_month.

lv_month = lv_month + 1.

  • wf_end_month = lv_month - 2.

FIELD-SYMBOLS: <fs> TYPE ANY.

DO 12 TIMES.

lv_month = lv_month + 1.

wf_end_month = lv_month - 2.

IF wf_end_month = end_date+4(2).

wf_month_ind = 'Y'.

ELSE.

wf_month_ind = 'N'.

ENDIF.

ASSIGN COMPONENT lv_month OF

STRUCTURE tfacs TO <fs>.

IF sy-subrc <> 0 OR lv_month = 15.

EXIT.

ENDIF.

lv_str = strlen( <fs> ).

DO.

IF wf_cnt = lv_str OR

( wf_cnt GE end_date+6(2) AND

wf_year_ind = 'Y' AND

wf_month_ind = 'Y' ).

wf_exit = '1'.

EXIT.

ENDIF.

wf_check_day = <fs>wf_cnt(1). " wf_check_monthwf_cnt(1).

IF wf_check_day = 1.

ADD 1 TO number_of_days.

ENDIF.

ADD 1 TO wf_cnt.

ENDDO.

IF wf_cnt GE end_date+6(2) AND

wf_year_ind = 'Y' AND

wf_month_ind = 'Y' AND

wf_exit = '1'.

EXIT.

ENDIF.

CLEAR: wf_cnt.

ENDDO.

wf_year = wf_year + 1.

CLEAR: wf_month, wf_end_month.

ADD 1 TO wf_month.

ADD 3 TO wf_end_month.

  • If record doesn't exist

IF sy-subrc NE 0.

RAISE no_calendar_for_year.

ENDIF.

  • IF wf_month > end_date+4(2) AND

  • wf_year_ind = 'Y' AND

  • wf_month_ind = 'Y'.

  • EXIT.

  • ENDIF.

ENDWHILE.

ENDFUNCTION.

Use the following parameters:

*"----


""Local interface:

*" IMPORTING

*" REFERENCE(CALENDAR) LIKE TFACS-IDENT

*" REFERENCE(START_DATE) LIKE SY-DATUM

*" REFERENCE(END_DATE) LIKE SY-DATUM

*" EXPORTING

*" REFERENCE(NUMBER_OF_DAYS) TYPE I

*" EXCEPTIONS

*" INVALID_DATE

*" NO_CALENDAR_FOR_YEAR

*"----


Create a new FM and copy this code snippet there and then you may reuse it.

Hope this is useful.

Regards

Harsh

Read only

sunilkumar_reddy
Discoverer
0 Kudos
5,182

This message was moderated.