function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pankaj kabrapankaj kabra 

Difference Between Two Dates Excluding Weekends

//Class for Calculating Working Dates between Two Days
public with sharing class TotalWorkingDyasBetweenTwoDates {
     public static Double totalWorkingDays(Date startDate,Date endDate){
        Double totalWorkingDays = 0;
        Double temps; 
        Date tempDate = date.newInstance(1985, 6, 24); 
        //Calculating mod of no of days between random date and start date
        Double temp = Math.mod(tempDate.daysBetween(startDate) , 7);
        if(temp==0){
             System.debug('hello 0');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 2;
            }
            else if(temps == 2 ){
                totalWorkingDays = 3;
            }
            else if(temps == 3 ){
                totalWorkingDays = 4;
            }
            else if(temps == 4 ){
                totalWorkingDays = 5;
            }
            else if(temps == 5 ){
                totalWorkingDays = 5;
            }
            else if(temps == 6 ){
                totalWorkingDays = 5;
            }
            else
                totalWorkingDays = 1;
        }
       else if(temp==1){
                    System.debug('hello 1');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 2 ){
                totalWorkingDays = 3;
            }
            else if(temps == 3 ){
                totalWorkingDays = 4;
            }
           else  if(temps == 4 ){
                totalWorkingDays = 5;
            }
            else if(temps == 5 ){
                totalWorkingDays = 4;
            }
            else if(temps == 6 ){
                totalWorkingDays = 5;
            }
            else
                totalWorkingDays = 1;
            
        } 
       else if(temp==2){
                    System.debug('hello 2');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 2 ){
                totalWorkingDays = 3;
            }
           else if(temps == 3 ){
                totalWorkingDays = 3;
            }
           else  if(temps == 4 ){
                totalWorkingDays = 3;
            }
           else  if(temps == 5 ){
                totalWorkingDays = 4;
            }
           else  if(temps == 6 ){
                totalWorkingDays = 5;
            }
            else
                totalWorkingDays = 1;
            
        } 
       else if(temp==3){
                    System.debug('hello 3');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 2 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 3 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 4 ){
                totalWorkingDays = 3;
            }
           else  if(temps == 5 ){
                totalWorkingDays = 4;
            }
           else  if(temps == 6 ){
                totalWorkingDays = 5;
            }
            else
                totalWorkingDays = 1;
           
        } 
       else if(temp==4){
                    System.debug('hello 4');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 1;
            }
           else  if(temps == 2 ){
                totalWorkingDays = 1;
            }
           else  if(temps == 3 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 4 ){
                totalWorkingDays = 3;
            }
           else  if(temps == 5 ){
                totalWorkingDays = 4;
            }
           else  if(temps == 6 ){
                totalWorkingDays = 5;
            }  
            else
                totalWorkingDays = 1;          
        } 
        else if(temp==5){
                     System.debug('hello 5');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 0;
            }
           else  if(temps == 2 ){
                totalWorkingDays = 1;
            }
           else  if(temps == 3 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 4 ){
                totalWorkingDays = 3;
            }
           else  if(temps == 5 ){
                totalWorkingDays = 4;
            }
            else if(temps == 6 ){
                totalWorkingDays = 5;
            }
            else
                totalWorkingDays = 0;
            
        } 
       else if(temp==6){
                    System.debug('hello 6');
            temps =  Math.mod(startDate.daysBetween(endDate) , 7);
            if(temps == 1 ){
                totalWorkingDays = 1;
            }
           else  if(temps == 2 ){
                totalWorkingDays = 2;
            }
           else  if(temps == 3 ){
                totalWorkingDays = 3;
            }
           else  if(temps == 4 ){
                totalWorkingDays = 4;
            }
          else   if(temps == 5 ){
                totalWorkingDays = 5;
            }
            else if(temps == 6 ){
                totalWorkingDays = 5;
            }
            else
                totalWorkingDays = 0; 
        } 
        else{
                System.debug('sffsf');
                totalWorkingDays = 999;
        }
       
        totalWorkingDays = totalWorkingDays + math.floor(startDate.daysBetween(endDate)/7*5);  
        return totalWorkingDays;
    }
 }
dinesh jaindinesh jain

Ok..good one

sourav046sourav046

I am not going inside code.Just a simple fundamental .

 

Do you know how to configure holidays in salesforce calender ?

With this you  can define custom holidays .

Try this :Your Name>Setup>Administration Setup>Company Profile>Holidays

 

Also try Business Hours for the same .

 

 

dinesh jaindinesh jain
But i need to calculate total working day for my company. The method you told just configure thee holidays. how to calculate in terms of number of holidays.
sourav046sourav046

Okay .Tell  me one more thing is it necessary for you to use APEX code ?What if I suggest you WF rule ?

dinesh jaindinesh jain
Actually i am in training of salesforce. I developed this apex code from formula Itself. Tell ME WF Rule also. It will be Benificial for me
sourav046sourav046

Ok .Thats very brave confession .

 

Let me tell you try your best  to use WF rules(i.e WorkFlow rules)  to achieve what you want.If you see thats not possible anyhow using WF rules then only go for APEX class or triggers .Always try to simplify things.

 

Now tell me is it ok if I work for you using WF rules ?

pankaj kabrapankaj kabra

Agreed...

Sourav :-)