0

Business hours is set to 7 AM to 7 PM Mon to Fri, Time Zone (GMT-05:00) Eastern Standard Time (America/New_York).

Business Hours

I need to simply add 5 working days from Current Date in order to calculate the target Date. I simply used BusinessHours.add API.

Long bMilliSec = 5*12*60*60*1000;
System.debug('LONG VALUE :: '+bMilliSec);
Date startDate = Date.today();
DateTime startDateTime = DateTime.newInstance(startDate.year(), startDate.month(), startDate.day());
BusinessHours bh = [SELECT Id,Name,IsActive,IsDefault,MondayEndTime,MondayStartTime 
                    FROM BusinessHours 
                    WHERE Name = 'ZZZZZZ'];
DateTime businessEndDateTime = BusinessHours.add(bh.Id,startDateTime,bMilliSec);
System.debug('########## :: '+businessEndDateTime);

The below 2 days are holidays:

  • 2/14/2018 All Day
  • 2/12/2018 All Day

Why would I get a result of 22-Feb-2018 instead of 21-Feb-2018?

debug log

Some testing around this..

I created an Adhoc Request (for estimated completion date) is 2 days which should be 2/14/2018 but instead it’s displaying 2/13/2018. I created a content request,(for estimated completion date) is 2 days is 5 days which should be 2/19/2018 but instead it’s displaying 2/21/2018.

Some Crazy Calculations are happening

  • if I add 2 days = 2018-02-17 00:00:00
  • if I add 3 days = 2018-02-20 00:00:00
  • If I add 4 days = 2018-02-21 00:00:00
  • If I add 5 days = 2018-02-22 00:00:00
  • Try using Datetime.now() instead of your current approach to getting startDateTime. – Adrian Larson Feb 12 '18 at 16:01
  • Still the result is same. No changes – Sayali Shinde Feb 12 '18 at 16:03
  • I tried the same setup and getting 21-Feb-2018. Are you sure about the holidays and the Business Hours mentioned in the question. – Saroj Bera Feb 12 '18 at 22:07
  • What is the UserInfo.getTimeZone() of the user running the Apex? – Daniel Ballinger Feb 13 '18 at 0:09
0

As per salesforce docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm, and https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm#apex_methods_system_datetime

I am formatting date which returns : Converts the date to the local time zone and returns the converted date as a formatted string using the locale of the context user. If the time zone cannot be determined, GMT is used..

global static Date getBusinessTargetDate(DateTime startDate, Integer businessDays){
    DateTime businessEndDateTime;
    DateTime startDateTime = DateTime.newInstance(startDate.year(), startDate.month(), startDate.day());

    BusinessHours bh = [SELECT Id,Name,IsActive,IsDefault,MondayEndTime,MondayStartTime 
                        FROM BusinessHours 
                        WHERE Name = 'US Business Hours'];
    businessEndDateTime = BusinessHours.add(bh.Id, startDateTime, businessDays*9*60*60*1000L);

    String strDateValue = businessEndDateTime.format().split('\\s+')[0];
    String[] strArray = strDateValue.split('/');

    return Date.newInstance(Integer.valueOf(strArray[1]), Integer.valueOf(strArray[0]), Integer.valueOf(strArray[2]));
}

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.