+ Start a Discussion
Santosh Shahi04807505290019439Santosh Shahi04807505290019439 

I want to calculate business hours between two dates , how we will calculate it by apex code ,(business hours standard object)

Ramu_SFDCRamu_SFDC
The below posts might help

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008vmkIAA

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008mR5IAI
Martha_SenetaMartha_Seneta
I think this is the method you're looking for:

BusinessHours.diff(String businessHoursId, Datetime startDate, Datetime endDate)

For more info you might want to just look up the BusinessHours Class in the standard Apex reference:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm
Carolina Ruiz MedinaCarolina Ruiz Medina
Hi Santos,
I had the code below,   that maybe it could help. However I like more the use of BusinessHours class. The solution that @Martha_Senete suggested is nice. ( I clicked like on that answer :) ) 


-->Datetime startDate = datetime.newInstance(2014, 01, 01, 10, 0, 0 );
--> Datetime endDate = datetime.newInstance(2014, 01, 01, 18, 0, 0);

public Time calculateBusinessHours(Datetime startDate, Datetime endDate)
 {
       
        Long businessHours = endDate.getTime() - startDate.getTime();
   
        return dateTime.newInstance(businessHours).time();

 }

Anyway Hope this help too.

Kind Regards,
Carolina.