How to get number of days, hours, minutes between two datetimes?
Last Update: November 23, 2017
Here are the steps on how to return number of days, hours, minutes between two datetimes:
- Open the form in
design mode.
- Add two DateTimes controls to the form design workspace and set its ID property to DateTime1 and DateTime2 respectively.
- Add TextBox control to the form design workspace and set its ID property to TextBox1.
- Select the DateTime1 control.
- In the
Rules pane, click on the Add icon to add a new rule, the
Rule Manager dialog will open.

- Add a new rule as follows:
A. Change the
Rule Name as desired.
B. Change the
Rule Type to Action.
C. Change the
Event Type to onChange. Leave "Execute if event triggered only on this control" unchecked.
D. In the
Condition section, type the following:
DateTime1 != '' && DateTime2 != ''
E. In the
Action section, type the following script:
var date_future = new Date(getValue(DateTime2));
var date_now = new Date(getValue(DateTime1));
// get total seconds between the datetimes
var delta = Math.abs(date_future - date_now) / 1000;
// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
// whats left is seconds
var seconds = delta % 60; // in theory the seconds is not required
if(DateTime1 > DateTime2)
setValue(TextBox2, concat('- ', days, ':', hours, ':', minutes));
else
setValue(TextBox2, concat(days, ':', hours, ':', minutes));
F. Click on the
Save button to save the rule.
Note: You can use
Assistance Panel to help you adding functions and related parameters, form variables and form controls to
Conditions and/or
Actions sections. - Test the form by clicking on the
Preview button in the
View Group.