0

How to calculate the business days difference between two dates in APex? I'm going to pass firstdate and finishdate from batch apex(below class is going to call in batch apex). I need to calculate only business days difference.

public with sharing class DaysDifferenceForTimeLog {
    public static integer EndDateStartDateDifferenceForTimeLog(date firstDate, date finishDate) {
        
        integer difference= firstDate.daysBetween(finishDate);
        return difference;
    }
}
1

1 Answer 1

1

try below :-

/*
 * Get numbe of business days between two dates
 *
 * */
public Integer getNoOfBusinessDaysBetweenDates(DateTime startDate, DateTime endDate){
    Integer count = 0;
    
    BusinessHours bHours = [SELECT Id FROM BusinessHours WHERE IsDefault = true limit 1];
     
    while(startDate <= endDate){
        if(BusinessHours.isWithin(bHours.Id, startDate)){
            count++;
        }
        startDate = startDate.addDays(1);
    }
    return count;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .