is now

Calculate the Total Number of Days Between Two Dates

Last update: June 27, 2025

Picture of Corey Wilson
Corey Wilson

Technical Writer

In this article:

Let’s create a Calculated Field that calculates the Total Number of Days Between Two Dates

Note: The App you create this Calculated Field in can vary based on your Servis.ai setup. We are using the Opportunities App for our example but a Contacts or Projects App works just as well.

To begin we will need the following fields in the Opportunity App:

  •  Date Created- A Date Field that contains the date the opportunity record was created.
  •  Date Closed- A Date Field that contains the date the opportunity record was marked as Closed.

See How to create a new form field for more. 

Begin by naming the Calculated Field and selecting what Section of the Opportunities App the Calculated Field will be displayed in (the choices will depend on your setup).

Next, click the Show Advanced Settings box and change the Calculated field to Yes

Now, select script Calculation Type. Enter the following code snippet into the Script window:

(function(opportunity){

var StartDate = Date.parse(opportunity.Created);

var EndDate = Date.parse(opportunity['Close Date']);

var timeBetween = EndDate - StartDate;

var days = timeBetween / (24 * 60 * 60 * 1000);

var justDays = parseInt(days);

return justDays;

}(opportunity));

Your fields may have different names. Replace the bold field names in the code snippet with the names of your fields. 

Save the Calculated Field.

Add Validation

You could add validation for this calculation to return a ‘0’ if the Close Date is not available. To do so, add an ‘IF’ statement, as shown in bold below.

(function(opportunity){

var StartDate = Date.parse(opportunity.Created);

var EndDate = Date.parse(opportunity['Close Date']);

if(!EndDate){

return 0;

}

var timeBetween = EndDate - StartDate;

var days = timeBetween / (24 * 60 * 60 * 1000);

var justDays = parseInt(days);

return justDays;

}(opportunity));

🙄

😐

😊

😍

Welcome to servis.ai Free Edition

Link your email to begin

Continue with Google

Continue with Microsoft

By continuing, you agree to servis.ai Terms of Use. Read our Privacy Policy.

Get Started with servis.ai

30-minute demo where you see servis.ai in action.

Unlock the essential servis.ai features at no cost.

How can I be of servis?
How can I be of servis?