When working with Salesforce data, it's often necessary to filter or report on records from a specific quarter. If you're looking to retrieve the start and end dates of the previous quarter based on the current date, here’s a simple and reusable Apex utility you can use.
🔧 Apex Utility Methods
Below are two helper methods:
-
getStartDate()
— Calculates the start date of the previous quarter. -
getEndDate()
— Calculates the end date of the previous quarter, based on the start date.
🧠Explanation
-
The start month of the current quarter is calculated using:
((currentMonth - 1) / 3) * 3 + 1
Then we subtract
3
to move to the previous quarter.-
We adjust for year boundaries (e.g., if today is in Q1, the previous quarter is in the previous year).
-
The end date is simply 3 months after the start, minus one day
✅ Example
If today's date is April 25, 2025
, then:
getStartDate()
will return January 1, 2025
getEndDate()
will returnMarch 31, 2025
Comments