1

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++;
            }
        }
        return d.addDays(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 d.addDays(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.

1 Answer 1

1

From the docs for BusinessHours.add() (emphasis mine):

Adds an interval of time from a start Datetime traversing business hours only.

"Traversing business hours only" means that it completely skips non-business hours. For example, assume your business hours are 9 AM to 5 PM Monday to Friday (and no hours on the weekend). If you start with a date time of Friday at 4:55 pm and add 10 minutes, the result will be 9:05 AM the following Monday.

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.

2
  • thanks for your answer on the business hours. I created a custom method to skip weekends and holiday, but when adding negative days it is not calculating correctly (see my method above) that I added.
    – Dman100
    Apr 23, 2015 at 15:52
  • got it figured out. It was the GMT conversion. It is working now.
    – Dman100
    Apr 24, 2015 at 18:13

You must log in to answer this question.

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