0

This could be the very simple use case, but I am straggling to make this working. How we can find out the business working days difference between two dates ? I know 8/26/2017 is Saturday, next days would be Sunday, again 2nd & 3rd Sept is weekends, so Ideally difference should be 6 days right ?

  • Case-1: Including Holidays
  • Case-2: Excluding holidays

Here is the code :

Date todaysDate = Date.today();
Date newDate = Date.today().addDays(10);
Integer noOfWorkingdaysDiff = todaysDate.daysBetween(newDate);
System.debug('No. Of Working Days : '+noOfWorkingdaysDiff);
  • This is not a trivial task, it's even less so if you want to make it work from one year to the next without having to make changes to your code. Variations of this question have been asked before (take a look over at the right side of your screen, under the advertisement, where it says "Related"). – Derek F Aug 26 '17 at 19:02
  • I'd recommend starting with the more simple task of trying to find the number of weekdays between two dates. A few hints to get you started: - Every 7 day interval will contain at least 2 weekend days. - Figuring out which day of the week a particular date is can be done in Apex. In the Date class documentation, toStartOfWeek() can be helpful - Things get easier after 21 days of difference because every 7 day interval in between the first and last weeks will have exactly 2 weekend days. – Derek F Aug 26 '17 at 19:04
1

For excluding holidays, Configure Business Hours excluding holidays and calculate time difference as below

 public static Integer getBusinessHourTimeDifference(Datetime startDate, Datetime endDate)
    {
        BusinessHours defaultBH = [SELECT Id From BusinessHours WHERE Name = 'your business hour name'];
        Long inputMillisecs =  BusinessHours.diff(defaultBH.Id, startDate, endDate);
        Integer total_duration_in_seconds = (inputMillisecs/1000).intValue();       
        return math.mod(math.floor(total_duration_in_seconds/86400).intValue(),365); 
    }

Refer Business Hours

For including holidays you have already done.

  • Assume I have 3 business hours then how we can tweak this code ? – user48562 Aug 26 '17 at 19:50
  • based on name you can find that – Santanu Boral Aug 26 '17 at 19:57
0

I tried to build the logic like below way, Since I am not sure whether we're going to have a holidays details in the salesforce setting or not, thats why answer to Case-1 is below

This works fine. Link for reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Map<Integer,String> ignore = new Map<Integer,String>(); 
ignore.put(6, 'Saturday');
ignore.put(7, 'Sunday');

Date todaysDate = Date.today();
Date newDate = Date.today().addDays(10);
Integer days = todaysDate.daysBetween(newDate);

DateTime dt = DateTime.newInstance(todaysDate.year(), todaysDate.month(), todaysDate.day());
DateTime ndt = DateTime.newInstance(newDate.year(), newDate.month(), newDate.day());
String weekDay = dt.format('EEEE');

Integer weeks = days/7;
Integer startDay = Integer.valueOf(dt.format('u'));
Integer endDay =  Integer.valueOf(ndt.format('u'));   
Integer diff = weeks * ignore.size();
System.debug('weeks :'+weeks+', startDay :'+startDay+',endDay : '+endDay+', diff :'+diff);

for(Integer dId : ignore.keySet()){
    if(startDay <= dId){
        diff++;
    }
    if(endDay > dId){
        diff++;
    }
}
if(endDay > startDay){
    diff -= endDay - startDay;
}

System.debug(days - diff);

Now I need to look at the ways for Case-2, might someones answer will help me here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy