Date Diff Years
Introduction
This function subtracts one date from another date and returns a numeric value in years, such as 2 years, 5 years, 15 years, or 24 years. While calculating the difference between two dates, you can provide static dates, or the DATEDIFFYEARS function can dynamically pick dates from attributes and elements.
Use Cases
You can use the DATEDIFFYEARS function in the following functional scenarios:
- Checking user’s page in the banking and other common apps
- Calculating maturity age of related fix deposit and other investment schemes in Financial and banking apps
- Calculating employee’s total tenure in the organization in the payroll and other human resource applications
- Calculating the age of property for correct valuation and pricing
- Carrying out a cohort analysis on a group of users or customers since they had joined or signed up
Pre-requisite
- An application
- An entity
Concepts
This section describes how to use the DATEDIFFYEARS function to calculate the difference between two dates in years and then store its output in an attribute.
DATEDIFFYEARS Function
Input Parameters | Data Type | Description | Sample Value |
date1 | String | This is the first date. The year of the second date is subtracted from the year of the first date. If the first date is greater than the second date, the function returns the positive value. If the first date is less than the second date (future date), the function returns the negative value. | 26-04-2025 |
format1 | Datetime | It represents the date format of the first date. The permissible date formats are: dd-mm-yyyy, dd-mm-yy, yyyy-mm-dd, yy-mm-dd, dd-mm-yyyy hh:mm:ss, and others | dd-mm-yyyy |
date2 | String | This is the second date. The month of the second date is subtracted from the month of the first date. | 14-06-2023 |
format2 | Datetime | It represents the date format of the second date. The permissible date formats are: dd-mm-yyyy, dd-mm-yy, yyyy-mm-dd, yy-mm-dd, dd-mm-yyyy hh:mm:ss, and others | dd-mm-yyyy |
Output Value | Description |
A numeric value | The DATEDIFFYEARS function returns the different between two dates in years. |
Example
This section briefly explains how to calculate the duration of the user in the current organization. The output of the DATEDIFFYEARS function is stored in the attribute. To implement this functionality, you must have:
- An application page that contains the following elements:
- Date Picker Element (Label: Start Date in Current Company)
- Button (Label: Submit)
- An entity that contains the following structure:

Usage of DATEDIFFYEARS Function
- Create a task group named CurrDateTime. Thus, in the task group, define the DATEDIFFYEARS function in the Copy Data task. Select the currDateTime attribute in the place of date1 parameter and currCompTime attribute in the place of date2 parameter.

- Link the CurrDateTime task group to the onClick event of the Submit button.
When the user clicks the Submit button, the DATEDIFFYEARS function subtracts the value of the currCompTime attribute from the value of the currDateTime and creates the output in years. It stores the output in the totalTenure attribute.
Best Practices
To use the DATEDIFFYEARS function efficiently, you are recommended the following set of best practices:
- When you use the element and define entities and attributes, use easy-to-remember reference names and names so that you select them without a hassle while mapping in the smart selector.
- To implement a specific application scenario, make a habit of creating a new task group. Do not use a single task group for multiple functional scenarios. This approach makes the application design clutter and complex.
- If you apply multiple tasks in a task group, apply them in a correct sequence so that the required workflow remains intact.
- If you use multiple tasks in a single task group, you are recommended to use delay function between two tasks so that the application has sufficient timeframe to do two or more consecutive tasks sequentially.
- When you are using entities, use a clean and light-weight JSON structure of the entity.
- Before you use the entity in the application, link it to the data model.
Troubleshooting
If using the DATEDIFFYEARS function does not return the expected result, check:
- If you define the correct value of dates and their formats.
- If you defined the correct mapping between elements and attributes or selected the correct element to link the related event to the task group.
- Check the sequence of different tasks in the task group.
- Check the value of first and second date to achieve the desired results.
- If you use an API for data manipulation, make sure you establish the correct mapping between the API’s parameters and elements or attributes.
- If you use an API, check its configuration on the vConnect portal.