Recently, while working as a Salesforce developer, I received a task to add days, months, and years to a date field. The requirement was to automate the user subscription trial extension.
When new customers sign up for a product trial, they receive a 14-day trial period by default. However, the sales team offers a 7-day extension to customers upon request. We had to update the customer’s trial end date by adding seven extra days to their current trial end date.
In this Salesforce tutorial, I will explain how to add days, months, and years to a Date field in Salesforce Apex using various methods.
Add Days to a Date Field in Salesforce Apex
In Salesforce Apex, we can use the addDays() method to add days to the date field.
Now, execute the code below in the Salesforce Apex anonymous window to add days to a date field in Salesforce Apex.
Date today = Date.today();
Date myDate = today;
Date futureDate = myDate.addDays(10);
System.debug('Today\'s Date: ' + today);
System.debug('Future Date: ' + futureDate);In the above code, “Date futureDate = myDate.addDays(10)” will add ten days to the date value that is today’s date in the code.
Now, in the execution log window, we will see that the number of dates is added to the date value for which we used the addDays() method.

This way, we can add the number of days to a date field in Salesforce Apex using the addDays() method.
Add Months to a Date Field in Salesforce Apex
In Salesforce Apex, we can use the Date.addMonths() method to add months to a date field. This method uses an integer as an argument, representing the number of months to add in the date field.
Now, execute the code below in the Apex anonymous window to add months to a date field.
Date today = Date.today();
Date futureDate = today.addMonths(3);
System.debug('Today\'s Date: ' + today);
System.debug('Future Date (after adding 3 months): ' + futureDate);In the above code, Date.today() will get today’s date, and today.addMonths(3) will add three months to the date value.
Now, in the execution log window, you will see today’s date and the date value with the added three months in the debug.

This way, we can add the number of months to the date field in Salesforce Apex using the Date.addMonths() method.
Add Months to a Specific Date in Salesforce Apex
We can use the Date.addMonths() to add months to a specific date field like, 23/10/2024.
Execute the code below in the Salesforce Apex anonymous window to add months to a specific date field.
Date SpecificDate = Date.newInstance(2024, 10, 16); // date in "yyyy/m/dd" format
Date futureDate = SpecificDate.addMonths(5);
System.debug('Original Date: ' + SpecificDate);
System.debug('Future Date (after adding 5 months): ' + futureDate);Now, in the execution log window, the debug will show the output for the entered date field and the date field with the added number of months.

This way, we can add months to a specific date value in Salesforce Apex using the addMonths() method.
Add Years to a Date Field in Salesforce Apex
In Salesforce Apex, adding years to the date field is similar to adding days and months. To add years to a date field in Salesforce Apex, we use the Date.addYears() method.
Now, execute the code below in the Apex anonymous window to add the years to a date field.
Date today = Date.today();
Date futureDate = today.addYears(2);
System.debug('Today\'s Date: ' + today);
System.debug('Future Date (after adding 2 years): ' + futureDate);In the above code, we have used the Date.today() to get the current date, and to add two years to the date, we have used today.addYears(2). Instead of today’s date, you can use any specific date with the expression Date.newInstance(2024, 11, 26).
In the execution log’s debug, you will see the current date value and the date value with the number of added years.

This way, we can add a number of days to a date field in Salesforce Apex using the Date.addYears() method.
Conclusion
In this Salesforce tutorial, we have discussed several methods for adding days, months, and years to the date field in Apex. We used methods such as Date.addDays(), Date.addMonths(), and Date.addYears() to add the number of days, months, and years to the current and specific date values.
You may also like to read
- Extract Year, Month, and Day from a Date Field in Salesforce Apex
- Convert Date to DateTime Format in Salesforce Apex
- Convert DateTime to Date Format in Salesforce Apex
- Convert Unix Timestamp to DateTime using Apex in 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.