+ Start a Discussion
magdielhfmagdielhf 

Add business days with Apex

Given the Start Date and No. of Business days  find ending date, if you pass a negative amount of days then should get Current Date less those business days,

 

Already test it and currently using it, feel free to correct or improve it for sharing

 

    public static Boolean IsWeekendDay(Datetime dateParam)
    {
       boolean result     = false;
        
       //Recover the day of the week
       Date startOfWeek   = dateParam.date().toStartOfWeek();
       Integer dayOfWeek  = dateParam.day() - startOfWeek.day();
           
       result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
        
       return result;
    } 
    
    
    public static Datetime AddBusinessDays(Datetime StartDate, integer BusinessDaysToAdd )
    {
       //Add or decrease in BusinessDaysToAdd days 
       Datetime finalDate = StartDate;
       
       integer direction = BusinessDaysToAdd < 0 ? -1 : 1;

        while(BusinessDaysToAdd != 0)
        {
            finalDate = finalDate.AddDays(direction);            
            
            if (!isWeekendDay(finalDate))
            {
                BusinessDaysToAdd -= direction;
            }
        }

        return finalDate;
    } 

 

 

dboonepesdboonepes

I modified this code so you can give a date parameter rather than a datetime parameter.

 

 public static Boolean IsWeekendDay(Date dateParam)
    {
       boolean result     = false;
       system.debug('dateParam = '+dateParam); 
       //Recover the day of the week
       Date startOfWeek   = dateParam.toStartOfWeek();
       system.debug('startOfWeek = '+startOfWeek);
       Integer dayOfWeek  = dateParam.day() - startOfWeek.day();
       system.debug('dayOfWeek = '+dayOfWeek);   
       result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
       system.debug('result = '+result); 
       return result;
    } 
    
    
    public static Date AddBusinessDays(Date StartDate, integer BusinessDaysToAdd )
    {
       //Add or decrease in BusinessDaysToAdd days 
       Date finalDate = StartDate;
       system.debug('finaldate = '+finalDate);
       integer direction = BusinessDaysToAdd < 0 ? -1 : 1;
       system.debug('direction = '+direction);
        while(BusinessDaysToAdd != 0)
        {
            finalDate = finalDate.AddDays(direction);
            system.debug('BusinessDaysToAdd = '+BusinessDaysToAdd);            
            system.debug('finaldate = '+finalDate);
            if (!isWeekendDay(finalDate))
            {
                BusinessDaysToAdd -= direction;
                
            }
        }

        return finalDate;
    }

 

SalesforceDevkuSalesforceDevku

This code is good, but it required a slight modification.

 

The reason being the scenario when the Start of the week is 05/28/2013 and the end of the week would be 06/03/2013.

 

In that case the difference between the dates would be 22. This scenarios would break the above code to check the Weekend day. So i would rather suggest you to use 

 

public static Boolean IsWeekendDay(Date dateParam)
{
boolean result = false;
//Recover the day of the week
Date startOfWeek = dateParam.toStartOfWeek();
Integer dayOfWeek = startOfWeek.daysBetween(dateParam);
result = dayOfWeek == 0 || dayOfWeek == 6 ? true : false;
return result;
}

 

magdielhfmagdielhf
Thanks for sharing guys, just got back this old account,

Good catch, hope it saved some time.
João Baptista 3João Baptista 3
I did some changes to code - just for IsWeekendDay - proposed below where were solved the following issues:
- The problem of being in Europe or US, where the week starts on Monday and on Sunday respectively, thus testing dayOfWeek against 0 or 6 only works for local regions similar to US. What is there now is little 'trick' using a datetime  conversion do Date in English language to guarantee we always catch Saturdays and Sundays.
- For last, in the month's crossing the formula wasn't detecting the weekend days, because the StartofWeek would be higher than the dateParam.day(), so in this cases it's necessary to add the total days of the month (in respect to the previous month of that day that we are treating)

 
public static  Boolean IsWeekendDay(Date dateParam){
        boolean result     = false;
        system.debug('dateParam = '+dateParam); 
        //Recover the day of the week
        DateTime dateT = DateTime.newInstance(dateParam, Time.newInstance(0, 0, 0, 0));
        Date startOfWeek   = dateParam.toStartOfWeek();
        system.debug('startOfWeek = '+startOfWeek);
        Integer dayOfWeek  = (dateParam.day()< startOfWeek.day())? (dateParam.day()+ date.daysInMonth(dateParam.year(),dateParam.month()==1? 12 : dateParam.month()-1))- startOfWeek.day(): dateParam.day() - startOfWeek.day();
        system.debug('dayOfWeek = '+dayOfWeek);   
        result = (dateT.format('E') == 'Sun' || dateT.format('E') == 'Sat') ? true : false;
        system.debug('result = '+result); 
        return result;
    }