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
stuckonforcestuckonforce 

Formula to create number of days between 2 dates

I was looking for a quick formula to calculate the number of days between a create and resolve date.

 

Any help would be appreciated.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

So you want a formula which finds a difference between 2 given dates? Have tried simply finding the difference between 2 dates by simple minusung them , that should work.

In case you think that you need to find which date is greater while substraction just use the following:-

 

if(d1>d2, d1-d2,d2-d1)

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

Ispita_NavatarIspita_Navatar

So you want a formula which finds a difference between 2 given dates? Have tried simply finding the difference between 2 dates by simple minusung them , that should work.

In case you think that you need to find which date is greater while substraction just use the following:-

 

if(d1>d2, d1-d2,d2-d1)

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

This was selected as the best answer
Steve :-/Steve :-/

What is the DataType of the fields you are using?  Are they Date, Date/Time, are they both the same, or is one a Date and the other a Date/Time?  

 

If both fields are the same datatype then you can use a simple formula that subtracts the Created Date from the Resolution Date and returns a number.  If they are not the same datatype then you are going to have to convert one of the fields to match the datatype of the other, and then do your calculation.

stuckonforcestuckonforce

Hi,

 

They are date fields.  One is a creation date and the other is a resolution date. I am trying to calculate the days to resolve.

 

On another note if I want to create a workflow alert if the creation date plus 2 days if the resoluiton date = null, but the rule didn't like the null bit.

 

any help would be great.

 

Thank you

stuckonforcestuckonforce

Worked like a charm...  Thank you very much...

Steve :-/Steve :-/

Okay here's a simple Duration (Days) formula that I did that you can borrow. 

 

Datatype:  Formula(Number,1)

[X] Treat blank fields as zeros 

Formula: 

 

End_Date__c - Start_Date__c

 

 

AbhayAbhay

public void showDays(String startDate, String enddate){        
        date strtDate = date.parse(startDate);        
        date ndDate = date.parse(enddate);        
        integer totalDays = strtDate.daysBetween(ndDate);
        integer i;
        integer totalWorkingDays;
        totalWorkingDays=totalDays;
        for(i=1;i<=totalDays;i++){      
                
             Datetime dt1 = datetime.newInstance(strtDate.year(), strtDate.month(),strtDate.day());
             strtDate=strtDate.addDays(1);
             String dayString = dt1.format('EEEE');
             
             if(dayString=='Saturday'){
                 totalWorkingDays=totalWorkingDays-1;                 
             }
             if(dayString=='Sunday'){
                 totalWorkingDays=totalWorkingDays-1;
             }                    
        }     
        System.debug('Total Days'+totalWorkingDays);
    }