Calculate the difference between 2 dates easily with the below code. You can calculate the difference between 2 dates on a VF page itself or in Apex code. Salesforce provides standard methods to give the difference between 2 dates. To refer to the Salesforce Date Class Click Here.
For calculation of the difference between 2 datetimes Click Here.
To find the days between 2 date fields, Checkout the below code:
- Visualforce Page
1 2 3 4 5 6 7 |
<apex:page standardController="Account" showHeader="false" sidebar="false"> <apex:outputText value="Days : {! (TODAY() - account.SLAExpirationDate__c)}"></apex:outputText> <apex:outputText value="Hours : {! (TODAY() - account.SLAExpirationDate__c)*24}"></apex:outputText> <apex:outputText value="Minutes : {! (TODAY() - account.SLAExpirationDate__c)*24*60}"></apex:outputText> </apex:page> |
- APEX
1 2 3 4 5 6 |
Date startDate = Date.today(); Date endDate = Date.today().addDays( 30 ); Integer days = startDate.daysBetween( endDate ); system.debug( 'Days between both dates is ' + days ); |
Also Check:
For any queries or suggestions, comment below.
Cheers … Happy Coding … 🙂