​​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:

  1. ​​Open the form in design mode.
  2. Add two DateTimes controls to the form design workspace and set its ID property to DateTime1 and DateTime2 respectively.
  3. Add TextBox control to the form design workspace and set its ID property to TextBox1.
  4. Select the DateTime1 control.
  5. In the Rules pane, click on the Add icon to add a new rule, the Rule Manager dialog will open.
    How to auto-shifting Panel control in a form2.jpg
  6. 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.
  7. Test the form by clicking on the Preview button in the View Group.