Calculate DateTime Field Difference in Salesforce : Bijay Kumar
by: Bijay Kumar
blow post content copied from SalesForce FAQs
click here to view original post
### Summary of the Content This guide explains how to calculate the difference between two DateTime fields in Salesforce, focusing on methods to find this difference both in hours and minutes, as well as in days, hours, minutes, and seconds. It highlights the use of advanced formulas and Salesforce functions like FLOOR, MOD, ROUND, and TEXT to achieve accurate results. #### Key Points: 1. **Purpose**: To find the difference between two DateTime fields using Salesforce formulas. 2. **Example**: Calculating the difference between "5-03-2022 9:53" and "6-03-2022 10:58" results in "25 Hours 05 Minutes". 3. **Salesforce Functions**: Functions such as FLOOR and MOD are essential for accurate calculations. 4. **Formulas**: - To calculate hours and minutes: ```salesforce IF (FLOOR((EndDateTimeField-StartDateTimeField)*24)>9,...) ``` - To calculate days, hours, minutes, and seconds: ```salesforce IF (FLOOR((EndDateField-EndDateField)) > 0, TEXT(FLOOR((EndDateField-EndDateField))... ``` 5. **Setup Steps**: The process to create a formula field in Salesforce Lightning, including setup through the Object Manager. 6. **Practical Application**: Users can create a "Total Time Period" field that shows the difference once the DateTime fields are filled. #### Additional Context: Understanding how to manipulate DateTime fields in Salesforce is crucial for reporting and managing data accurately, especially in environments that rely on precise time tracking, such as customer relationship management. ### Hashtags for SEO: #Salesforce #DateTime #DataManagement #SalesForceTutorial #SalesforceFormulas #CalculatingTime #CRM #LightningExperience #TechTutorial #SalesforceAdmin
I got a task at work that required me to calculate the difference between the date and time fields using Salesforce. To fulfill this requirement, I define an advanced formula.
In this Salesforce Tutorial, we will learn how to calculate DateTime field difference in Salesforce. Additionally, we will review how to use Salesforce Lightning to distinguish between datetime fields.
Calculate DateTime Field Difference in Salesforce
In Salesforce, there are many scenarios where we may need to get the difference between two datetime fields.
For example, we want to calculate the difference between 5-03-2022 9:53 and 6-03-2022 10:58 and want to display the result as: 25 Hours 05 Minutes.
For this, we will find the difference between the day, hours, minutes, and seconds of two datetime values by subtracting one datetime from the other.
But, if we directly subtract the datetime value, we will get the difference of the dates in fractions. To calculate the difference, we need to use some of Salesforce’s built-in functions, such as FLOOR, MOD, ROUND, and TEXT.
Additionally, the difference between two datetime fields in Salesforce returns the result in Both Number and text formats.
We have two different advanced formulas for the difference between two datetime fields in Salesforce. We can either calculate the difference between two datetime fields in hours and minutes, or we can calculate the difference between two datetime fields in days, hours, minutes, and seconds.
Suppose you want to find the difference in hours and minutes. Here is the formula:
IF
(
FLOOR((EndDateTimeField-StartDateTimeField)*24)>9,
TEXT(FLOOR((EndDateTimeField-StartDateTimeField)*24)),
"0" & TEXT(FLOOR((EndDateTimeField-StartDateTimeField)*24))
)
&"."&
IF
(
ROUND(MOD((EndDateTimeField-StartDateTimeField)*1440,60),0)>9,
TEXT(ROUND(MOD((EndDateTimeField-StartDateTimeField)*1440,60),0)),
"0" & TEXT(ROUND(MOD((EndDateTimeField-StartDateTimeField)*1440,60),0))
)
Calculate Hours:
- In this formula, to get the number of hours, we multiply the difference between the two dates in days by 24.
- Then, we use the FLOOR function to round down the result to the closest integer.
- If the number of hours is less than 10, we also use an IF statement to add a 0 to the result so that we get 09 rather than just 9.
Calculate Minutes:
- In this formula, to get the number of minutes, we multiply the difference between the two dates in days by 1440. Then, we use the MOD function to determine the remainder of a division.
- The ROUND function is also used to eliminate decimal points.
- We also use an IF statement to add a 0 to the result so that we get 09 rather than just 9 if the number of minutes is less than 10.
Note: We use the & character to join these two parts together.
If you want to find the difference in days, hours, minutes, and seconds. Here is the formula:
IF (FLOOR((EndDateField-StartDateField)) > 0, TEXT(FLOOR((EndDateField-StartDateField)) ) & " Days ", "")
&
IF(FLOOR(MOD((EndDateField-StartDateField)* 24, 24 )) > 0, TEXT(FLOOR(MOD((EndDateField-StartDateField)* 24, 24 ))) & " Hours ","")
&
TEXT(ROUND(MOD((EndDateField-StartDateField)* 24 * 60, 60 ), 0)) & " Minutes "
&
TEXT(ROUND(MOD((EndDateField-StartDateField)* 24 * 60*60, 60 ), 0)) & " Seconds"
Calculate Days:
- In this formula, to get the number of days, we calculate the difference between the two datetime fields.
- Then, we use the FLOOR function to convert the fractional result to the whole number of days.
- If the number of days is greater than 0, we use the IF statement to convert the result to a text string using the TEXT function, and then append the text “Days” to it.
Calculate Hours:
- In this formula, to get the number of minutes, we multiply the difference between the two dates in days by 24.
- Then, we use the FLOOR function to round down the result to the closest integer, and the MOD function returns the remainder after dividing the difference between the two datetime multiplied by 24 by 24.
- If the number of hours is greater than 0, we use the IF statement to convert the result to a text string using the TEXT function, and then append the text “Hours” to it.
Calculate Minutes:
- In this formula, to obtain the number of minutes, we use the FLOOR function to round the result down to the closest integer.
- And the MOD function returns the remainder after dividing the difference between the two datetime values by 24*60*60.
- The resultant value is then converted to a text string using the TEXT function and appended with the text ‘Minutes‘.
Calculate Seconds:
- In this formula, to get the number of seconds, we use the FLOOR function to round down the result to the closest integer.
- The MOD function returns the remainder after dividing the difference between the two datetime values, multiplied by 24*60*60, by 60.
- The resultant value is then converted to a text string using the TEXT function and appended to the text string “Seconds”.
To create a text string that represents the time difference in the format of Days, Hours, Minutes, and Seconds, all four parts are combined using the & symbol.
Any components of the datetime field that differ from a value of 0 will not be displayed.
Salesforce Formula: Calculate DateTime Field Difference
To calculate the difference between two datetime fields in Salesforce Lightning, the following are the steps:
- Click the Setup icon in the top right corner of the page to access Salesforce Setup.
- Select Object Manager from the navigation bar.

- Look for the object whose field you wish to find the difference between two datetime fields. In this instance, I chose the Lead object from the list of objects.

- Select Fields & Relationships. Next, select New.

- Choose the Formula as the field type. Click Next to proceed.

- After entering a Field Label for the formula, the Field Name will be filled in automatically. Here, I specify the field label as Total Time Period. Select the Formula Return Type as Text. After that, select Next.

- In the Advanced Formula editor, enter the advanced formula to determine the difference between two datetime fields. The formula is as follows:
IF (FLOOR((End_Date_Time__c - Start_Date_Time__c)) > 0,
TEXT(FLOOR((End_Date_Time__c - Start_Date_Time__c)) ) & " Days ", "")
&
IF(FLOOR(MOD((End_Date_Time__c - Start_Date_Time__c)* 24, 24 )) > 0, TEXT(FLOOR(MOD((End_Date_Time__c - Start_Date_Time__c)* 24, 24 ))) & " Hours ","")
&
TEXT(ROUND(MOD((End_Date_Time__c - Start_Date_Time__c)* 24 * 60, 60 ), 0)) & " Minutes "
&
TEXT(ROUND(MOD((End_Date_Time__c - Start_Date_Time__c)* 24 * 60*60, 60 ), 0)) & " Seconds"
- The difference between two date/time fields called “End_Date_Time__c” and “Start_Date_Time__c” is calculated using this formula, which provides the result in days, hours, minutes, and seconds.
- First, we check if the number of days is greater than 0. It returns the number of days between the two dates, followed by the word Days. If not, it returns an empty string.
- Next, we calculate the number of hours between the two dates using the MOD function to get the remainder after dividing the time duration by 24 hours. If this value is greater than 0, it returns the number of hours followed by the word Hours. If not, it returns an empty string.
- To verify that the formula is valid, click Check Syntax. We can specify how you want to handle the empty field and include the Description and Help Text as optional fields.
- Then, select Next.

- Choose the appropriate field’s level of security, and then check the box beside visible to make it visible for everyone. Click Next.

- Select the Page Layout Name for which the formula field is to be stated.
- Click Save to save the formula field.

- Once we’ve set up the formula field, we may use it. Let’s examine an example:
- Open the Leads Tab and create an order with a Start Date and Time, and an End Date and Time, then save it.
- After that, click on the Details section, and the Total Time Period field will appear, showing the difference between the two Date/Time fields.

Conclusion
We now know how to calculate the difference between two datetime fields using the advanced Salesforce formula.
Furthermore, we discovered the formula to get the difference between DateTime fields using Salesforce Lightning.
You may also like to read:
- Salesforce Formula to get Last Day of Month
- Salesforce Formula DATETIMEVALUE
- Salesforce Weekday Formula
- Add Picklist Values in Salesforce
The post Calculate DateTime Field Difference in Salesforce appeared first on SalesForce FAQs.
September 11, 2025 at 06:58PM
Click here for more details...
=============================
The original post is available in SalesForce FAQs by Bijay Kumar
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment