**How to get day counts between two given dates excluding Saturday and Sunday.?**

Key for the given requirement is the **date** variable in ABAP itself. Actually date variable in ABAP is the count of days from 01.01.0001 to given date internally.

So if we pass the date variable value to an integer variable and display it, it will display the day count for 01.01.0001 to the given date.

e.g: DATA: lv_date TYPE d VALUE ‘00010201’,

lv_count TYPE i.

lv_count = lv_date.

WRITE:/ lv_count.

The output is 31.

Now to how to get the day of a date?

The answer again lies in the date variable. If we get the modulus of 7 for the date variable it ‘ll give the day as

2 -> Monday

3 -> Tuesday

4 -> Wednesday

5 -> Thursday

6 -> Friday

0 -> Saturday

1 -> Sunday

E.g. DATA: lv_date TYPE d VALUE ‘20120102′,

lv_count TYPE i.

lv_count = lv_date MOD 7.

WRITE:/ lv_count.

The Output is 2 i.e 01.01.2012 is a Monday.

Now the following Function Module can be used for getting the day count excluding Saturday and Sunday.

FUNCTION ZGET_DAYS_COUNT .

*”———————————————————————-

*”*”Local Interface:

*” IMPORTING

*” REFERENCE(IV_DATE_FROM) TYPE BEGDA

*” REFERENCE(IV_DATE_TO) TYPE ENDDA OPTIONAL

*” EXPORTING

*” REFERENCE(EV_NO_DAYS) TYPE INT4

*”———————————————————————-

* Technical Consultant : Rakesh M Mondal

* Date : 06.09.2010

* Description : Get Days Count between to dates excluding

* Saturday and Sunday

*”———————————————————————*

*Local Data declarations

DATA: lv_date_to TYPE endda,

lv_total_days TYPE i,

lv_day_from TYPE p,

lv_day_to TYPE p,

lv_week TYPE i,

lv_days TYPE i,

lv_fw_count TYPE i.

*~If To date is not given it ‘ll curent system date~*

IF iv_date_to IS INITIAL.

lv_date_to = sy-datum.

ELSE.

lv_date_to = iv_date_to.

ENDIF.

*Calculatin the total no of days between the given dates including the to date.

lv_total_days = lv_date_to – iv_date_from + 1 .

*~Determining the day of from date~*

PERFORM get_day USING iv_date_from CHANGING lv_day_from.

*~Determining the day of to date~*

PERFORM get_day USING lv_date_to CHANGING lv_day_to.

*if from date is not a monday then

*deduct the no of days till first monday

*and calculate the Saturday and sunday for that week.

IF lv_day_from NE 1.

lv_days = lv_total_days – ( ( 7 – lv_day_from ) + 1 ).

IF lv_day_from GT 6.

” If the day is Sunday then LV_FW_COUNT will be 1

lv_fw_count = 1.

ELSE.

* if it is Monday to Satuday then the LV_FW_COUNT will be 2.

lv_fw_count = 2.

ENDIF.

ELSE.

*if From date is a Monday then just pass the total no of days between

*From date to TO DATE

lv_days = lv_total_days.

ENDIF.

*if the To Date is not a sunday then

*deduct the no of days till last sunday

IF lv_day_to NE 7.

lv_days = lv_days – lv_day_to .

ENDIF.

*Calculating no of weeks for the remainig days

lv_week = lv_days / 7.

* Calculating total no of days excluding saturday and sunday

* = ( total no of day between the given intput ) –

* ( twice of week i.e no of Saturday and Sunday ) –

* ( Satuday and Sunday of the first week ).

ev_no_days = lv_total_days – ( 2 * lv_week ) – lv_fw_count.

ENDFUNCTION.

*&———————————————————————*

*& Form GET_DAY

*&———————————————————————*

* To get the day for the given date

* 2 -> Monday

* 3 -> Tuesday

* 4 -> Wednesday

* 5 -> Thursday

* 6 -> Friday

* 0 -> Saturday

* 1 -> Sunday

*———————————————————————-*

* –>P_Date Date

* <–P_DAY Day

*———————————————————————-*

FORM get_day USING p_date TYPE scdatum

CHANGING p_day TYPE p.

p_day = p_date MOD 7.

IF p_day > 1.

p_day = p_day – 1.

ELSE.

p_day = p_day + 6.

ENDIF.

ENDFORM. ” GET_DAY

Andrey VishnevskiyHello, Rakesh.

Could you explain please what is the use of your function module?

Rakesh Mohan MondalPost authorHello Andrei,

Its sample code only, I have use it for finding age of document (excluding Saturday and Sunday). The good part of this code is that it takes almost same time of execution for any set of dates.

Andrey VishnevskiyOk, I got it. But it only serves for exact purpose. I mean you calculate duration assuming that only Saturday and Sunday are weekends, right? What about holidays? Or countries where weekends are different from Saturday and Sunday? For instance, Israel has Friday and Saturday as weekends.

Reinventing the wheel is good. No, I’m not kidding. That is real good for understanding how the system works. And your code works well. And fast. But in general your task should be solved by FM DURATION_DETERMINE which returns duration between two days considering factory calendar.

Rakesh Mohan MondalPost authorWell your Point is fine. But Suppose that HR module is not implemented then, we can’t use factory calendar. Again it’s a simple sample of code not a Reinventing anything. It just a exploration a data type D

.

Andrey VishnevskiyYes, Rakesh. I got it. Good explanation of type D. With pretty useful example. Thanks.

BTW, you do not need HR module to use factory calendar and to use mentioned FM.

Rakesh Mohan MondalPost authorWell thank for the information.I’ll try to explore that ðŸ™‚

Syam BabuHi All,

it will count the no of days between the given date and end date without Saturday and Sunday..thanks Rakesh…. i got same requirement.

Thanks,

Syam.

mitul samaiyaHi Rakesh,

It is really very Usefull.

Hendrik BrandesHi Rakesh,

thank you for sharing this, but there is another quite simple and powerful function module FIMA_DAYS_BETWEEN_TWO_DATES which can handle your requirements and has some interesting options.

E.g.

Kind regards,

Hendrik

Rakesh Mohan MondalPost authorHi Hendrik,

Thanks for the input.Yes it can handle leap year correctly. Here you should look at the interesting fact of the Date variable in ABAP.

Regards,

Rakesh.