I'm trying to use APEX BusinessHours class to check whether a certain Date is a Business Day or Not.

Here is my Business Hour in my Organization: enter image description here

Based on this, if I provide following date input, it doesn't return correct value of whether my provided date is a Business Day or not:

date inputDate          = date.newInstance(2018, 2, 2); //2nd Feb, 2018 = FRIDAY
datetime inputDateGmt   = datetime.newInstanceGmt(inputDate, Time.newInstance(0, 0, 0, 0));

system.debug('@@... inputDate: ' + inputDate);
system.debug('@@... inputDateGmt: ' + inputDateGmt);

boolean isBusinessDay   = BusinessHours.isWithin('01m90000000KlnrAAC', inputDateGmt);
system.debug('@@... isBusinessDay: ' + isBusinessDay);

But my isBusinessDay variable returning True, given that 2-Feb-2018 is a Friday and Is Not a Business Day based on my Business Hours. The Timezone of my logged in user (Timezone: 'Asia/Kolkata') is different than BusinessHours Timezone.

Do we need to match the Timezone of Local User to the Business Hour Timezone? If so how do we check if a Day is business day or not for any user with different timezone using the APEX api function?

== Update 1 ==

My debugging output with datetime.newInstance() method call:

@@ ... inputDate: 2018-02-02 00:00:00
@@ ... inputDateGmt: 2018-02-02 00:00:00
@@ ... isBusinessDay: true

My debugging output with datetime.newInstanceGmt() method call:

@@ ... inputDate: 2018-02-02 00:00:00
@@ ... inputDateGmt: 2018-02-01 18:30:00
@@ ... isBusinessDay: true

And notice that, isBusinessDay is TRUE both times.

== Update 2 ==

When I update my inputDateGmt variable to datetime inputDateGmt = datetime.newInstanceGmt(inputDate, time.newInstance(8, 0,0,0));, then only I get isBusinessDay = FALSE.

  • Hopefully, you have received answer at your own – Santanu Boral Feb 1 '18 at 20:28
  • I may have. The solution I see is to initialize Time.newInstance() with Timezone offset from Business Hour's Timezone for the inputDate. But I am wondering if this is completely correct approach. Looks very rough to me but seems to give me correct result for now. Have to run thorough test to make sure if the concept works correct always. – VarunC Feb 1 '18 at 20:33
  • I'm wondering if the issue is related to the Kolkata timezone being UTC -5.5. Have you tried with a user in the PST timezone? – Daniel Ballinger Feb 1 '18 at 23:05
  • Yes. If i chose the user in same timezone as BusinessHour then I get expected result. Thus, I now add Timezone Offset of Business Hour into the GMT Time and then evaluate isWithin function to check if it is a business day. – VarunC Feb 2 '18 at 6:31

Hope you found an answer, but posting for others' edification.

Turns out that if the Business Hours are set to 24 hrs, everything "is within" business hours, including holidays. When I updated business hours for a day of the week to non-24 hour values, any holidays that fall on that day of the week will be considered "not within" business hours. You'll need to do this for all days of the week.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.