+ Start a Discussion
Baktash H.Baktash H. 

Time Difference



i have a small problem.


I need to know the difference between 2 datetimes in seconds.

One is a CreatedDate field, the other is datetime.now()


I tried converting them to timestamps to calculate with them but it didn't work.


Anyone have ideas?


Hi developer74,

Did you try convert the two dates to date value and then do the calculation.

Also when you subtract two dates the difference is calculated or returned in days which can be converted into seconds later.

Hope this helps.

Baktash H.Baktash H.

thanks for the anser but the conversion does not work.

i tried this:

Integer date1 = DateTime.getTime(myList[0].CreatedDate);


which i took from here:



But it says it does not know the method.


You can directly subtract the one datetime from another. Find below a formula to calculate the total number of minutes from two datetime fields:


Total number of seconds = (LastModifiedDate-CreatedDate)*24*60*60;

Baktash H.Baktash H.

actually i tried that.

Integer theNumber = Integer.valueOf(datetime.now()) - Integer.valueof(lastComment[0].CreatedDate);



it give me this nice message: