Salesforce Formula: Calculate Days Between Dates

Photo of author
Jerome Clatworthy

Certified Salesforce Administrator

This article will explain the Salesforce formula required to calculate the days between dates in Salesforce, as well as how to achieve it in both Salesforce Lightning and Salesforce Classic.

The process of calculating the number of days between two dates is very simple in Salesforce. It is simply a matter of subtracting one date from the other, in the Advanced Formula Editor (as long as the two dates are the same format). Some examples are provided below.

Salesforce Formula for Number of Days Between Dates

To calculate the number of days between two dates in Salesforce, we need to find the difference between the two date values in the number format by subtracting one date from the other.

The general formula for calculating the number of days between two dates in Salesforce is:

EndDateField - StartDateField

You need to start with the end date, or most recent date, as that will have a higher ‘numerical value’, and give you a positive integer.

The other way to calculate the number of days between two dates is to use the DATEVALUE function. This function converts the Date and Time values to date values. The formula using DATEVALUE is:

DATEVALUE(EndDateField) - DATEVALUE(StartDateField)

If you have a Date and Time field and we want to calculate only the days between two Dates, then use the DATEVALUE function.

KEY CONCEPT

It is important to note that both the StartDateField and EndDateField are the same type, either DateTime or Date. Otherwise, the formula will not work.

By using these formulas, we can easily calculate the number of days between two dates in Salesforce. Here are two examples using Salesforce Lightning and Salesforce Classic.

Formula to Calculate Days Between Two Dates in Salesforce Lightning

To calculate the number of days between two dates in Salesforce Lightning, follow these steps:

  1. Click on the “Setup” icon located in the top right corner of the page to access Salesforce Setup.
  2. Select “Object Manager” from the left side of the menu.
  3. Look for the object whose field you wish to get days between dates. In this instance, select the “Opportunity” object from the list of objects.
  4. Select “Fields & Relationships”.
  5. Next, select “New”.
  6. Choose the “Formula” as the field type.
  7. Click “Next” to proceed.
  8. After entering a Field Label for the formula, the Field Name will be filled in automatically. Specify the field label as “Created To Last Modified”.
  9. Select the Formula Return Type as “Number” and set the Decimal Places to “Zero”. Then, select “Next”.Screenshot of Salesforce Formula field creation screen.
  10. In the Advance Formula editor, enter the formula to determine the Days between two Dates. The formula is as follows:
LastModifiedDate - CreatedDate
Screenshot of Salesforce Custom Field Formula screen.
Select the fields for the formula
Screenshot of Salesforce Custom Field Formula.

In this example, the formula calculates the number of days between the “Created Date” and the “Last Modified Date”. Therefore, we subtract the older date (Created Date) from the newer date (Last Modified).

  1. To verify that the formula is valid, click “Check Syntax”.
  2. Specify “how you want to handle the empty field” and include the “Description” and “Help Text” as optional fields.
  3. Then, select “Next”.
  4. Choose the appropriate field’s level of security and check the box beside “visible” to make it visible to everyone.
  5. Click “Next”.
  6. Select the Page Layout Name for which the formula field is to be displayed.
  7. Click “Save” to save the formula field.

Once the formula field is set up, it can be used. Here is an example:

  1. Create an Opportunity Report.
  2. Add the new formula field to the Opportunity Report to see your new calculation in action. You could also add the field to a page layout if it would be useful to display there.
Screenshot of Salesforce Opportunity Report.

Common Issues With ‘Days Between Two Dates’ Salesforce Formulas

If you get a syntax error when trying to save your new formula, the most likely cause is that you are trying to use two different types of fields.

There are ‘Date’ fields, and there are ‘Date Time’ fields – and they cannot interact directly within formulas.

To use them, you will need to use a modified ‘DATEVALUE’ expression to extract the date value from a Date & Time field.

DATEVALUE(EndDateField) - DATEVALUE(StartDateField)

This will extract the date only, from the Date/Time field for use in your calculations.

Formula to Calculate Days Between Two Dates in Salesforce Classic

To calculate the number of days between two dates in Salesforce Classic, the exact same formula described above can be used, but you obviously used navigate the Classic user interface to create the new field, or add the formula to a row-level formula.

If you want a new formula field in Salesforce Classic, enter Setup, and choose the Object you want to add the field to from the left-hand menu in Customize.

Screenshot of Salesforce Classic object listing.
Select the Object you want to add the formula field to

Select Add a custom field to contacts from the available options.

Screenshot of Salesforce Classic Object customization screen.
Select ‘Add a custom field to contacts’

Then choose a new Formula field, as demonstrated in the previous example, and follow the same steps.

Screenshot of Salesforce Classic Object Manager creating a new Formula field.
Configure the new formula field the same way as the previous example

Other Uses For This Formula

In addition to its use in creating new fields that appear on reports and page layouts, this formula for the number of days between dates can be used to create a row-level formula in reports.

By subtracting one date from another, the resulting value will be the difference between the two initial values in days.

Salesforce Formula Frequently Asked Questions

How to Calculate the Number of Days Between a Date and Today in Salesforce Formula

To calculate the number of days between a date and today, you can subtract the date from the TODAY() function. The formula syntax is as follows:

TODAY() - Date__c

The result will be a number that represents the number of days between the date and today.

What is the Salesforce Formula to Calculate a Date Range

To calculate a date range in the Salesforce formula, you can add or subtract a certain number of days from a given date. The formula syntax is as follows:

Date__c + Number_of_Days__c

Make sure that the date is in the Date format and the number of days is a numeric value.

Adding a Certain Number of Days to a Date in a Salesforce Formula

To add a certain number of days to a date in the Salesforce formula, you can use the following formula syntax:

Date__c + Number_of_Days__c

Make sure that the date is in the Date format and the number of days is a numeric value.

How to Calculate the Number of Days Between Two Dates Using Salesforce Flow Formula

To calculate the number of days between two dates using the Salesforce Flow formula, you can use the following formula syntax:

{!End_Date__c} - {!Start_Date__c}

Make sure that both dates are in the same format, either Date or Date/Time. The result will be a number that represents the number of days between the two dates.