Date Diff Hours
Introduction
This function subtracts one date from another date and returns a numeric value in hours, such as 2 hours, 6 hours, 10 hours, or 15 hours. While calculating the difference between two dates, you can provide static dates, or the DATEDIFFMONTHS function can dynamically pick dates from attributes and elements.
Use Cases
You can use the DATEDIFFHOURS function in the following functional scenarios:
- To calculate the time difference between clock-in time and clock-out time in attendance application
- To calculate the total time taken to resolve customer support ticket or service request
- To measure elapsed time between task start and end time and then track the productivity or bottleneck
- To measure system downtime in monitoring portals
Pre-requisite
- An application
- An entity
Concepts
This section describes how to use the DATEDIFFHOURS function to calculate the difference between clock-in and clock-out time. You can frequently use this scenario in the digital attendance register and core human resource applications.
DATEDIFFHOURS 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 DATEDIFFMONTHS function returns the different between two dates in hours. |
Example
This section describes how to use the DATEDIFFHOURS function to calculate the difference between the clock-in and clock-out time of a user. To implement this functionality, you have:
- An application page that contains:
- Two buttons (Label: Clock In, Label: Clock Out)
- A text element that displays the difference between the clock-in and clock-out time
- An entity that stores the difference between the clock-in and clock-out time

Usage of DATEDIFFHOURS Function
- In the property panel of the Text element, click the Data tab and then select the totalTime attribute in the Entity Path box.
- Create a task group named Attendance_Tskgp. In this task group, define the CURRENTDATETIME function in the Copy Data task. Thus, map the output of the CURRENTDATETIME function to the clockIn attribute as follows:

- Create another taskgroup named Attendance_Tskgp_Clockout. In this task group, define the CURRENTDATETIME function in the Copy Data task. Thus, map the output of the CURRENTDATETIME function to the clockOut attribute.

- In the Attendance_Tskgp_Clockout task group, define the DATEDIFFHOURS function in another Copy Data task and then map its output to the totalTime attribute.

- Map the Attendance_Tskgp task group to the onClick event of Clock In button. Map the Attendance_Tskgp_Clockout task group to the onClick event of Clock Out button.
When the user clicks the Clock Out button, the DATEDIFFHOURS function calculates the difference between the recorded clock-in and clock-out times. Thus, the Copy Data task stores the difference in the totalTime attribute. Because the totalTime attribute is mapped to the text element, the text element displays the difference on the application page.
Best Practices
To use the DATEDIFFHOURS 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.
- 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 DATEDIFFMONTHS 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.