function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
D harishD harish 

how to calculate difference between working hours of a day in apex

Hi,

I am using Business hours object to calculate the difference between two days using apex.Here i am facing a problem to calculate the difference between start time and end time of a business day(Ex.difference between Mondayendtime and Mondaystarttime)
is there method to calculate the difference?

can someone post the syntax how to calculate the difference between start time and end time of a business day?

simple-forcesimple-force

Hi,

 

as I understand your question you want to calculate the difference between 2 dateTime variables.

 

 

Here is an example:

 

Long diff = DateTime.now().addMinutes(5).getTime() - DateTime.now().getTime();

// diff is in this case 300000 mili sec = 300 sec = 5 mins

//In your case

DateTime Mondayendtime = DateTime.now().addHours(-8);
DateTime Mondaystarttime = DateTime.now();

Long diff2 = MondayendtimegetTime() - Mondaystarttime.getTime();

diff2 = diff2/1000/60/60;

System.debug('diff in hours' + diff2);

 

 

Christian

Abhi_TripathiAbhi_Tripathi

Hey harish you can try this

 

this difference is in hours

 

currentStartOfWeek, finalStartOfWeek .........these are date fields.

 

//calculating final value
ageDifferece = ((finalStartOfWeek.getTime())/1000/60/60) - ((currentStartOfWeek.getTime())/1000/60/60);

 

Thanks

Abhi