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
Mohan SelvamMohan Selvam 

Difference between Time

In my Apex Code I have Default Start time and Default End Time named as start and stop, code as follows
 time start = time.newInstance(8,30,0,0);
 time stop = time.newInstance(17,0,0,0);

and I am getting start time and endtime from Object [field type DateTime] from that i am getting only time in Starttime and Endtime, code as follows

time Starttime = Time.newInstance(SSJ.Start_Date_and_Time__c.hour(),SSJ.Start_Date_and_Time__c.minute(),SSJ.Start_Date_and_Time__c.second(),SSJ.Start_Date_and_Time__c.millisecond());

 time Endtime = Time.newInstance(SSJ.End_Date_and_Time__c.hour(),SSJ.End_Date_and_Time__c.minute(),SSJ.End_Date_and_Time__c.second(),SSJ.End_Date_and_Time__c.millisecond());

Now i want to find the difference between
Stop to Starttime and
Start to Endtime . how to calculate this?

Thanks and Regards 
S.Mohan
 
Best Answer chosen by Mohan Selvam
Frédéric TrébuchetFrédéric Trébuchet
Hi,

You can simply compute the difference, expressed in milliseconds, using this code:
// Compute end - start returning result in milliseconds
int difference;
difference = 
   ((SSJ.End_Date_and_Time__c.hour() * 3600000) + 
   (​SSJ.End_Date_and_Time__c.minute() * 60000) + 
   (SSJ.End_Date_and_Time__c.second() * 1000) + 
   SSJ.End_Date_and_Time__c.millisecond()) -
   ((SSJ.Start_Date_and_Time__c.hour() * 3600000) + 
   (​SSJ.Start_Date_and_Time__c.minute() * 60000) + 
   (SSJ.Start_Date_and_Time__c.second() * 1000) + 
   SSJ.Start_Date_and_Time__c.millisecond());
Now, if you want the resut in a human readable format, have a look at http://stackoverflow.com/questions/175554/how-to-convert-milliseconds-into-human-readable-form.

Hope this helps,
Fred
 

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Hi,

You can simply compute the difference, expressed in milliseconds, using this code:
// Compute end - start returning result in milliseconds
int difference;
difference = 
   ((SSJ.End_Date_and_Time__c.hour() * 3600000) + 
   (​SSJ.End_Date_and_Time__c.minute() * 60000) + 
   (SSJ.End_Date_and_Time__c.second() * 1000) + 
   SSJ.End_Date_and_Time__c.millisecond()) -
   ((SSJ.Start_Date_and_Time__c.hour() * 3600000) + 
   (​SSJ.Start_Date_and_Time__c.minute() * 60000) + 
   (SSJ.Start_Date_and_Time__c.second() * 1000) + 
   SSJ.Start_Date_and_Time__c.millisecond());
Now, if you want the resut in a human readable format, have a look at http://stackoverflow.com/questions/175554/how-to-convert-milliseconds-into-human-readable-form.

Hope this helps,
Fred
 
This was selected as the best answer
Mohan SelvamMohan Selvam
Hi Frédéric Trébuchet

Thank you so much for your valuable reply... it helps me a lot...

Thanks and regards
S.Mohan
Mohan SelvamMohan Selvam

Hi Frédéric Trébuchet
I tried the same and i got my result. But if i am entering input start time as 10:30 AM and End time as 17:00 PM so the actual output is 6:30 Hrs but it is returning 6 Hrs. Please tell me what went wrong or what i shouls to the exact hours from milliseconds.
My code as follows,
FirstDiff=(((stop.hour()*3600000)+(stop.minute()*60000)+(stop.second()*1000)+(stop.millisecond()))-
                  ((Starttime .hour()*3600000)+(Starttime .minute()*60000)+(Starttime .second()*1000)+(Starttime .millisecond())))/(1000*60*60);
Frédéric TrébuchetFrédéric Trébuchet
Hi,

Declare FirstDiff as a decimal variable because 23400000/(1000*60*60) = 6.5 hours
Don't know what you want to do with the result but remember that presenting hours using decimal values may be confusing (6.5 hours = 6 hours and a half, 6.1 hours = 6 hours and 6 minutes, and so on).

Hope this helps,
Fred
Mohan SelvamMohan Selvam
Hi Frédéric Trébuchet

Thanks for your instant reply... I will explain my concepts 
Actually i am calculating Techncian Jobtime from Start time to End time.
Start time = 16/01/2015 8:30 am
End time = 16/01/2015 16:30 pm

So

Jobtime = 8:00 hrs
Frédéric TrébuchetFrédéric Trébuchet
So, using this method may help you to convert result to a human readable format:
public class convertTime {
	public static String convertMS(Integer ms) {
        Integer seconds = math.mod((ms / 1000), 60);
        Integer minutes = math.mod(((ms / 1000) / 60), 60);
    	Integer hours = math.mod((((ms / 1000) / 60) / 60), 24);

        String hhmm = string.valueof(100+hours).substring(1, 3) + ':' + 
                      string.valueof(100+minutes).substring(1, 3);
        system.debug(hhmm);
        return hhmm;
    }
}
This method is inspired from http://stackoverflow.com/questions/175554/how-to-convert-milliseconds-into-human-readable-form.
In this method, seconds are computed but not displayed in result. Just add them if needed, else, remove seconds computation.

Calling this method is pretty simple:
String jobTime;

jobTime = convertMS(firstDiff);
Hope this is what you are looking for.

Fred
Mohan SelvamMohan Selvam
Hi Frédéric Trébuchet

Thank you i will try this and let youknow the result...

Regards 
S.Mohan