This course is currently a work in progress. Please join the waitlist to receive regular updates.
Comparing dates in apex
To compare dates in Apex, use the logical comparison operators between two date variables.
Use the following operators:
Operator | Description | Example |
---|---|---|
== | Check if two dates are the same. | startDate == endDate |
!= | Check if two dates are not the same. | startDate != endDate |
< | Check if one date is less than another. | startDate < endDate |
> | Check if one date is greater than another. | startDate > endDate |
<= | Check if one date is less than or equal to another. | startDate <= endDate |
>= | Check if one date is greater than or equal to another. | startDate >= endDate |
How to compare two dates
To compare two dates in apex, use the logical operators listed above. The following is an example of checking if a customer is eligible for a return based on the purchase date and the current date.
Loading...
In the above example:
- The variable
dueDate
store today's date. - The variable
returnDate
has the 30th day from today. - The variable
canReturnItem
is a boolean that stores whether the due date is within the return period
Try It Yourself
Loading...