It’s always a challenge to Calculate business days or working days in Apex if you are not aware of BusinessHours object in Salesforce.
- APEX offers a very powerful object BusinessHours which can be used to get business days and exclude non-business days like weekends and holidays.
- However, the standard methods do not serve all use cases. As a result, I have created below utility class to serve all kind of use cases and scenarios.
- Add or Subtract only business days from a date.
- Exclude holidays and weekends from your date calculations.
- Get the next or previous working day
- Know if a specific day is a working day or not.
- Get the number of business days between 2 dates.
Before we jump to code, let’s read about BusinessHours object. You can configure your Organization’s Business hours using this object. This is easily accessible under the setup menu. Just navigate to Setup > Business Hours
- You can set up multiple business hours for your organization. For example, support team may work 24×7 while development team may only work during weekdays.
- You can set up multiple business hours depending on the geographical location of your team/office. For example, US team works in EST/PST timezone where Indian team works in IST timezone.
- You can even configure holidays and associate with your business hours. You can relate one holiday with multiple business hour records.

This utility class has 5 different methods to serve all kind of scenarios. Use these methods in combination to get your use case served 🙂
Method Definition
- getNextWorkingDay – Get next working day. If the date passed to this function is a working day, this will return the same date, else it will return next working day.
- getPreviousWorkingDay – Get previous working day. If the date passed to this function is a working day, this will return the same date, else it will return previous working day.
- addDays – Add business days to a date.
- subtractDays – Subtract business days from a date.
- isWorkingDay – Return if a specific day is working day or not.
- getNoOfBusinessDaysBetweenDates – Return number of business days between 2 dates.
BusinessDays Class
https://gist.github.com/046f10fa33a0befb977c306ee3b3c42c
- Setup business hours. There is already a default entry for every org.
- Initialize BusinessDays class.
- Using your business hour record name – BusinessDays bd = new BusinessDays(‘your business hour record name’);
- Using default record name – BusinessDays bd = new BusinessDays();
- Start using methods. Like to add 3 days in today’s date, bd.addDays(Date.today(), 3);
Please be aware this class focus more on Working Days and does not focus much on Working Hours. You can comment on this post if you have any use case where the calculation needs to happen on hours and you cannot achieve it using standard methods.
Read more on: BusinessHours Class
Pingback: Calculate Business Days In Apex – Utility Class – sfdc techie – Pavan's blog
vahidkhan
13 Nov 2018please calculate days with Half day PM and Half day AM..
Pingback: JSON to CSV Convert – Utility Class for Apex and Java – SFDCFacts
Stewart McNaught
28 Jan 2019Appreciate you sharing this utility class.
I have been trying to write a simple test class. However, I am receiving a ‘Maximum Stack Depth’ error in the Class.BusinessDays.getPreviousWorkingDay method. The method seems straight forward but I cannot seem to get it to work. Any thoughts on what I am doing wrong?
Here is my simple test class:
@isTest
public class BusinessDaysTest {
static testMethod void testMethod1() {
BusinessDays bd = new BusinessDays();
System.debug(‘Inserting Todays Date’ + Date.today());
Date today = Date.today();
DateTime startTime = DateTime.newInstanceGMT(today.year(), today.month(), today.day(), 2, 2, 3);
Test.StartTest();
// Add Days
System.debug(‘Starting Add Days Test For 10 Days’);
DateTime response1 = bd.addDays(startTime, 10);
System.assert(response1 != null);
// Sub Days
System.debug(‘Starting Subtract Days Test For 3 Days’);
DateTime response2 = bd.subtractDays(startTime, 2);
System.assert(response2 != null);
// Is Working Day
System.debug(‘Starting Working Day Test – Is Final Add Day a Workday’);
Boolean response3 = bd.isWorkingDay(response1);
System.assert(response3 != null);
//Days Between
System.debug(‘Starting to Counting Days between Final ADD Date and Subtract Date’);
Integer response4 = bd.getBusDaysBetween(response1, Date.today());
System.assert(response4 != null);
}
}
Gayathri
22 May 2019Hi,
I am not able to view BusinessDays Utility Class on the Post. Is it removed?
Manish Choudhari
8 Jun 2019No, it is still there. Are you not able to see it?
Saurabh Sood
7 Jul 2019Hi Manish,
This is very useful post but my scenario is to calculate the business hours and check calculated business hour is lies under business timing.
I use below method :
BusinessHours.add(buss.id, Date, timeInterval);
and it works !!