Find your content:

Search form

You are here

Date Calculation in Apex?

 
Share

I am lost on how to Calculate the difference between two dates (days):

temp_LatestGapLen = temp_PB_CurrSubBeg - temp_PrevSubEnd;

Left variable is defined as Integer, the other two are Dates. Error received is:

Error: Compile Error: Date arithmetic expressions must 
use Integer or Long arguments at line 449 column 53 

Help? I can't find a reliable reference to this online in my searches despite how simple this seems. Is it implying you can only do date vs integer calculations, not date vs date??

Thanks so much.


Attribution to: AMM

Possible Suggestion/Solution #1

Use daysBetween

date startDate = date.newInstance(2008, 1, 1);
date dueDate = date.newInstance(2008, 1, 30);
integer numberDaysDue = startDate.daysBetween(dueDate);

Here ist the doc: Date Methods


Attribution to: Sergej Utko

Possible Suggestion/Solution #2

Apex provides a native Date instance method, daysBetween().

date startDate = 
 date.newInstance(2008, 1, 1);
date dueDate = 
 date.newInstance(2008, 1, 30);
integer numberDaysDue = 
 startDate.daysBetween(dueDate);

In your case it would be

temp_LatestGapLen = temp_PB_CurrSubBeg.daysBetween(temp_PrevSubEnd);

Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/5239

My Block Status

My Block Content