• The issue of working days is much larger than just weekends.

    There are national holidays as well, plus unexpected "holidays" like extreme weather conditions and such.

    In our app we have separate tables for 3 types of "non-working days" and we use our own SQL functions to make working day calculations.

    This is a bit tricky with regards to what these functions return when one of the arguments is a non-working day. For example Monday 17th minus Friday 14th is one working day. But what should Monday 17th minus Sunday 16th return? Zero or One?

    There is some calculus problem here. If Sat-Fri = 0 and Sun-Sat = 0 and Mon-Sun = 0 then (Sat - Fri) + (Sun-Sat) + (Mon-Sun) = (Mon-Fri) = 0. Oupsss...

    The same issue appears if you define (Sat-Fri=1).

    It is a tough call, but if you ignore theory and stick to practice, here is how we decided to do it:

    PRINT Calendar.fn_AddWorkingDays('2012/08/05', 1) ==> Aug 6 2012 12:00AM

    PRINT Calendar.fn_AddWorkingDays('2012/08/04', 1) ==> Aug 6 2012 12:00AM

    PRINT Calendar.fn_AddWorkingDays('2012/08/03', 1) ==> Aug 6 2012 12:00AM

    PRINT Calendar.fn_DiffWorkingDays('2012/08/06', '2012/08/05') ==> 0

    PRINT Calendar.fn_DiffWorkingDays('2012/08/05', '2012/08/04') ==> 0

    PRINT Calendar.fn_DiffWorkingDays('2012/08/04', '2012/08/03') ==> 0

    Also remember another dirty detail. Your app must NOT go crazy if the company decides to work on a Saturday 🙂 Make sure you don't produce too many unexpected results. For example what should (Sat + 0) return?