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
VANAPARTHI SRIKANTHVANAPARTHI SRIKANTH 

Code to calculate difference between time of two dates

Hi All,

I am a salesfoce learner can any one help me.

can anyone write a code for calculating time between two different dated time for example:- difference  between time of 

20/7/2015 1:26 AM and 25/7/2015 5:20 PM.

can any one help in writing a code for me
Vivek DeshmaneVivek Deshmane
Hi VANAPARTHI,

You can use the getTime method to get the milliseconds between them and then convert to whatever unit you need:

Ex1.
Double var = Math.Floor((Double.valueOf(mem.End__c.getTime()) - Double.valueOf(mem.Start__c.getTime())) / (1000.0*60.0*60.0));
Ex2.
Long dt1Long = DateTime.now().addDays(-1).getTime();
Long dt2Long = DateTime.now().getTime();
Long milliseconds = dt2Long - dt1Long;
Long seconds = milliseconds / 1000;
Long minutes = seconds / 60;
Long hours = minutes / 60;
Long days = hours / 24;
Please let me know if this helps you.

Best Regards,
-Vivek