+ Start a Discussion
sfadm sfadmsfadm sfadm 

Days between two DateTime values excluding weekends


Hello,

I need to calculate hours between two DateTime objects in apex excluding non-working days and weekends. Here is my method:
// calculate working hours between createdDate and completedDate
private Integer calculateWorkingHours(Datetime createdDate, Datetime completedDate) {
    List<BusinessHours> bhList = [Select id From BusinessHours Where IsDefault=true];
    if(bhList.size() == 0) {
        //If there is a problem to get hours from the BusinessHours database table return -1
        return -1;
    }

    // Get id of used set of business hours
    String bhId = bhList.get(0).id;

    // Returns the difference in milliseconds between a start and end Datetime based on a specific set of business hours.
    System.debug('createdDate ' + createdDate);
    System.debug('completedDate ' + completedDate);
    Long businessMillis = BusinessHours.diff(bhId, createdDate, completedDate);
    //Convert milliseconds to hours
    System.debug('businessMillis ' + businessMillis); 
    Integer bHours = (Integer)((businessMillis/1000)/60)/60;
    System.debug('bHours ' + bHours);
    return bHours;
}
And the data from the BusinessHours database table:

User-added image

Method 
BusinessHours.diff()
is calculating the Saturday and Sunday such as 24 hours working days!

How do I change Saturday and Sunday to be non-working days, hence zero working hours?
Best Answer chosen by sfadm sfadm
PreyankaPreyanka
Hello,

Could you please check your business hours. If no hours is selected in your business hours for saturday and sunday then the time will not be calculated in BusinessHours.diff() and also holiday attached with business hours also exculded from calculation. To do so clear the content of Saturday and Sunday in respective business hours and save it.

Note: Only one business hours is default at a time hence it will be good if you use the name of the business hours in the query as shown below.

Select id From BusinessHours Where IsDefault=true and Name='Name of the business hour'

Thanks

All Answers

PreyankaPreyanka
Hello,

Could you please check your business hours. If no hours is selected in your business hours for saturday and sunday then the time will not be calculated in BusinessHours.diff() and also holiday attached with business hours also exculded from calculation. To do so clear the content of Saturday and Sunday in respective business hours and save it.

Note: Only one business hours is default at a time hence it will be good if you use the name of the business hours in the query as shown below.

Select id From BusinessHours Where IsDefault=true and Name='Name of the business hour'

Thanks
This was selected as the best answer
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,

May I request you please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar