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
Baktash H.Baktash H. 

Time Difference

 Hello,

 

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?

IspitaIspita

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:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

 

But it says it does not know the method.

Pradeep_NavatarPradeep_Navatar

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:

java.util.GregorianCalendar[time=1322078572979,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2011,MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=327,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=2,SECOND=52,MILLISECOND=979,ZONE_OFFSET=0,DST_OFFSET=0]