# problem with buiness hours add method

I'm trying to use the business hours add method, but the datetime coming back is not what I am expecting.

I'm trying like this:

`BusinessHours.add(bh.Id, DateTime.newInstance(dt.year(), dt.month(), dt.day(), 12, 0, 0, -(day * 60 * 60 * 24 * 1000))`

I want to get the business hours of a datetime I pass in and subtract a certain number of days...similar to using addDays(-3), but with business hours so it skips weekends and holidays.

Isn't a single day in milliseconds 86400000 and if it was negative days it would be 86400000?

I built a custom method to skip weekends and holidays:

``````public static Date addBusinessDays(Date d, map<Date, DateTime> mapholidays, integer DayDifference)
{
if(DayDifference > 0)
{
integer counter = DayDifference;
for(integer i = 1; i <= counter; i++) {
DateTime dt = DateTime.newInstance(d.addDays(i), Time.newInstance(0, 0, 0, 0));
String dayOfWeek = dt.format('EEEE');
if(dayOfWeek.toUpperCase() == 'SATURDAY' || dayOfWeek.toUpperCase() == 'SUNDAY' || mapholidays.containsKey(d.addDays(i)))
{
counter++;
}
}
}
else
{
integer counter = DayDifference;
for(integer i = -1; i >= counter; i--)
{
DateTime dt = DateTime.newInstance(d.addDays(i), Time.newInstance(0, 0, 0, 0));
String dayOfWeek = dt.format('EEEE');
if(dayOfWeek.toUpperCase() == 'SATURDAY' || dayOfWeek.toUpperCase() == 'SUNDAY' || mapholidays.containsKey(d.addDays(i))) {
counter--;
}
}

}
return null;
}
``````

However, there seems to be a problem with when adding negative days. It is going back one extra day and on certain dates it doesn't seem to skip the weekends correctly? When adding days it works fine, but is not correctly skipping when adding negative days. I haven't been able to figure out the issue with the method that is causing that to happen?

Any ideas?

Thanks for any help.

From the docs for `BusinessHours.add()` (emphasis mine):
There is no method to add business days, and the number of hours you would need to add depend on the business hours defined in your org. If they are the same (or the same number of hours) for every day you are open, you could use a formula like your question, but changing `24` to the number of hours you are open each day. If your hours vary (e.g., 9-5 Mon-Fri, 9-1 Sat), that won't work. It may be possible to calculate what you need with the methods that are available (e.g., `BusinessHours.isWithin()`, `BusinessHours.nextStartDate()`), but the solution depends on your business hours.