Calculation formulas on forms can use JavaScript date methods to add time to a date or calculate the difference between two dates. These examples assume that the referenced form fields are configured as date fields and have export keys available to the calculation.
⭐ Get Inspired
This article was adapted from a post by Technolutions staff in the Slate Community Forums' Get Inspired space. Have a great idea for a Get Inspired post? Let us know!
Add one year to a date
Use the following pattern to add one year to a date and return the result in yyyyMMdd format. Replace date_export_key with the export key of your date field.
(@date_export_key.getFullYear() + 1) + ('0' + (@date_export_key.getMonth() + 1)).slice(-2) + ('0' + @date_export_key.getDate()).slice(-2)The formula uses:
getFullYear()to return the four-digit year.getMonth()to return the month. JavaScript months are zero-indexed, so the formula adds1.getDate()to return the day of the month..slice(-2)to keep month and day values two digits long.
Calculate the difference between two dates
JavaScript stores the difference between dates in milliseconds. Divide the date difference by the appropriate number of milliseconds for the desired unit.
Days
Math.floor((@date1 - @date2) / 86400000)Weeks
Math.floor((@date1 - @date2) / 604800000)Years
Math.floor((@date1 - @date2) / 31557600000)📝 Note
The year example uses 365.25 days to account for leap years. Month calculations are not included because month lengths vary.
Test the output
Preview the form with sample date values.
Confirm that the calculation updates when the source date changes.
Submit a test record and verify the stored value in the form response.
📖 Learn more about JavaScript date methods.