In our Salesforce org, the sales team needs to create follow-up tasks for opportunities that were closed within the last 30 days. To determine whether an opportunity was closed within the last 30 days, we need to find the difference between the closed date and the current date.
As a solution, I created an apex class that compares the current datetime value with the datetime when the opportunity was closed.
In this Salesforce tutorial, I will explain how to compare two datetime values in Salesforce Apex.
Compare Two DateTime Values in Salesforce Apex
In Salesforce Apex, there are many scenarios where it is required to compare two datetime values. You might want to check if two datetime values are equal, if one is earlier or later than the other, or if there is a difference between them.
In the steps below, we will discuss various methods for comparing two datetime values for different scenarios.
Compare DateTime Values in Salesforce Apex Using the DateTime.getTime() Method
In the steps below, we will compare two datetime values using the datetime.newInstance() method. In the debug, we will show the output of the datetime value that exceeds the other.
Now, open the Salesforce developer console and execute the code below in the Apex anonymous window.
DateTime dt1 = DateTime.newInstance(2024, 11, 16, 10, 0, 0);
DateTime dt2 = DateTime.newInstance(2024, 11, 14, 12, 0, 0);
Long time1 = dt1.getTime();
Long time2 = dt2.getTime();
if (time1 == time2) {
System.debug('Both DateTime values are equal.');
} else if (time1 < time2) {
System.debug('dt1 is earlier than dt2.');
} else {
System.debug('dt1 is later than dt2.');
}In the above code, we have entered date values d1 = 2024/11/16 and d2 = 2024/11/14. Here, d1 is the later date than d2.
According to the conditionals in the above code, the debug output is displayed as dt1 is later than dt2.

This way, we can compare two datetime values in Salesforce Apex using the DateTime.getTime() method and adding conditionals.
Compare DateTime Values in Salesforce Apex using the DateTime.sort() Method
In Salesforce Apex, we can use the DateTime.sort() method to calculate the difference between two DateTime values. This method returns the difference in milliseconds. Therefore, we should use this method to compare close DateTime values that require comparing the time component of the DateTime field.
In the code below, I will compare the datetime fields System.today() and System.Now() and calculate the difference between these two in milliseconds.
Now, open the Salesforce developer console and enter the code below in the Apex anonymous window and execute it.
Datetime Date1 = System.now();
Datetime Date2 = System.today();
List<Datetime> sorted = new List<Datetime>();
sorted.add(Date1);
sorted.add(Date2);
sorted.sort();
System.debug(sorted);
System.debug('The difference between first and second date is: ' + (Date1.getTime() - Date2.getTime()) + ' milliseconds');In the execution log window, we will see the values of the Datetime field System.Now() and System.Today(), and also the difference between them in milliseconds.

This way, we can compare and sort two datetime fields in Salesforce Apex using the Datetime.sort() method in Salesforce Apex.
Compare two String DateTime Values in Salesforce Apex using the DateTime.valueOf() method
If the datetime values are in string format, we can use the valueOf function to convert strings to Datetime. Then, we can add the getTime method with conditionals to compare the datetime values.
Execute the code below in the Salesforce Apex anonymous window to compare string datetime values.
String stringdate1 = '2023-10-10 12:30:00';
String stringdate2 = '2023-10-14 14:45:00';
Datetime firstDate = Datetime.valueOf(stringdate1);
Datetime secondDate = Datetime.valueOf(stringdate2);
Long dt1 = firstDate.getTime() / 1000;
Long dt2 = secondDate.getTime() / 1000;
if((dt1 - dt2) < 0){
System.debug('First date is earlier than second date');
} else {
System.debug('First date is later than or equal to second date');
}In the above code, the valueOf method will convert the strings to a datetime. Then, using the getTime()/100, we convert the Datetime to a UNIX timestamp (in seconds). Then, using the conditionals, we will compare the values.
Now, according to the entered datetime values and debug statement, the debug will show which date value is earlier or later.
This way, we can compare the string datetime values in Salesforce Apex using the valueOf() and getTime() methods.
Calculate the Difference Between Two datetimes in Hours, Minutes, or Days in Salesforce Apex
In this example, we will take two specific DateTime values, StartDate and EndDate. Then, we will find the difference between Hours, Minutes, and Days of the two datetime values.
Now, execute the code below in the Salesforce Apex anonymous window.
DateTime startDate = DateTime.newInstance(2020,1,1,3,0,0);
DateTime endDate = DateTime.newInstance(2020,1,3,6,0,0);
System.debug('Diifrernce between the' + startDate + 'and' + enDate + 'is');
Decimal minutes = Integer.valueOf((endDate.getTime() - startDate.getTime())/(1000*60));
System.debug('Minutes : ' + minutes);
Decimal hours = Integer.valueOf((endDate.getTime() - startDate.getTime())/(1000*60*60));
System.debug('Hours : ' + hours);
Decimal days = Integer.valueOf((endDate.getTime() - startDate.getTime())/(1000*60*60*24));
System.debug('Days : ' + days);In the above code, the Datetime.getTimeI() method will output in milliseconds. To calculate the difference, we need to convert the millisecond values to minutes, hours, and days, and for that, we will use the mathematical expressions below.
- Dividing by 1000 * 60 converts milliseconds into minutes.
- Dividing by 1000 * 60 * 60 converts the time difference from milliseconds to hours.
- Dividing by 1000 * 60 * 60 * 24 converts the milliseconds difference to days.
Now, in the execution log window, we will see the difference in day, hours, and minutes between the DateTime values startdate and endDate.

This way, we can calculate the difference between two datetime values in hours, minutes, or days in Salesforce Apex.
Conclusion
In this Salesforce tutorial, we have learned how to compare two DateTime values in Salesforce Apex using various methods. In the above examples, we have used various methods, such as DateTime.getTime(), DateTime.sort() ( for comparing and sorting), and DateTime.valueOf() for string DateTime values.
In addition, we also discussed the method for finding the difference between the Hours, Minutes, and Days of two DateTime values.
You may also like to read.
- Convert Date to DateTime Format in Salesforce Apex
- Trigger Context Variable in Salesforce Apex
- Convert String to Datetime in Salesforce Apex
- Get Current Datetime in Apex Salesforce

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.