Salesforce: Adding specific working days to a Date

Many time while coding we come across the requirement in which we need to calculate the Enddate from a StartDate after specific number of business/working days. That means excluding the weekends (Saturday & Sunday) Following is the code snippet which does this. I know there can be optimize way for doing this. But I didn’t wanted to spend more time on this.

Below method takes date and number of working days to be added to the date.

global Datetime addBussinessDays(Datetime startDate, Integer iDays)
Datetime endDate = startDate.addDays(iDays);
Integer iOffDays = daysOff(startDate,endDate);
return endDate.addDays(iOffDays);

Method daysOff calculates the number of non-working days coming in the start date and end date. And after which we are again adding those number of days.

global Integer daysOff(Datetime sdate, Datetime edate)
Integer iCount = 0;
while (sdate < edate) {
if (sdate.format('E') == 'Sat' | sdate.format('E') == 'Sun'){
iCount = iCount + 1;
sdate = sdate.addDays(1);
return iCount;

  1. Hi Prasanna . I’m veryt thanks for your colaboration in Salesforce Code.
    If you have material about the basic treene, please send for me

  2. It should be noted this snippet does not take into account national/bank holidays, it just excludes weekends days. Otherwise, very nice.

  3. Hi

    After adding daysOff to the endDate what if the date comes out to be a weekend?
    You’ve not handled that situaution.

  4. Hi Chirag, Current code should be handling it. 🙂

  5. Hi If I want to exclude Fri and Sat as weekends and modify this formula so that it calculates the number of working days instead of a date, what changes do I need to make to this formula?

  6. Hi can You provide it in the form of A Trigger

Leave a Reply

%d bloggers like this: