Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi I tried subtracting Business hours. But somehow it is not working properly. Below is the code snippet that I used.

date mydate = date.parse('11/13/2012');
Datetime dt =datetime.newInstanceGMT(mydate.year(), mydate.month(),;
List <BusinessHours> bhList =  [SELECT Id FROM BusinessHours WHERE IsDefault = true] ;
Datetime newDt = BusinessHours.addGMT(bhList[0].id, dt, -(1*24* 60 * 60 * 1000));
System.debug('====='+dt+'===='+(double)BusinessHours.diff(bhList[0].id, newDt,dt)/(1 * 24 * 60 * 60 * 1000L));
System.debug('========='+(double)BusinessHours.diff(bhList[0].id, datetime.newInstanceGMT(2012,11,13),datetime.newInstanceGMT(2012,11,12))/(1 * 24 * 60 * 60 * 1000L));`enter preformatted text here`

Few assumptions in Business Hours: All days other than sunday and saturday, working hours is 24 hours and for sunday and saturday No hours.

Problem : The above code is returning 2012-11-11 00:00:00(Sunday) for 2012-11-13 00:00:00(Tuesday) and business hours difference for this two date is showing .796

Debug log of the above code :

12:08:51.033 (33130000)|USER_DEBUG|[5]|DEBUG|=====2012-11-13 00:00:00=====Tuesday=>>>==2012-11-11 00:00:00====Sunday
12:08:51.033 (33443000)|USER_DEBUG|[6]|DEBUG|=====2012-11-13 00:00:00====0.7916666666666666
12:08:51.033 (33753000)|USER_DEBUG|[7]|DEBUG|=========-0.7916666666666666

Please help.

share|improve this question

The difference is coming up as expected, if you add the following two lines of debugging:

System.debug(1*24* 60 * 60 * 1000);
System.debug(BusinessHours.diff(bhList[0].id, newDt,dt));

You will see that the diff is equal to the amount of time you're adding.

share|improve this answer

Most of the BusinessHours Examples have the input startDate in the local timezone rather than GMT.

It might be that the addGMT method is adjusting the dt input as though it was local time.

share|improve this answer
Well just wondering how to take everything in gmt – Avidev9 Nov 7 '12 at 12:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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