Tips & Tricks - Calculating the Date Difference

This post shows how to calculate the duration of a time period using JavaScript. This requires an input field for the start date and another input field for the end date. You can download the sample application here. It can then be imported as usual. Enable the advanced options so that all the dialog boxes mentioned in this example are accessible.

Sample Application in the Browser

When you open the sample application in your browser, you can enter the start and end dates of the time period to be calculated on the home page. Then click "Calculate." The result is displayed in the browser console.

Structure of the Sample Application

The sample application contains only one page, where you'll find the two required input fields for the start and end of the time period. Clicking the "Calculate" button runs the script that calculates the duration.

Double-clicking the button opens the Properties dialog box. There, under the "Script" tab, you'll find the function call that is assigned to the onclick event and is written as follows:

            calculateDateDuration("10B7488AC1684253320AE33B03DD5FF5EF50ED2A", "D86D01178C27065D49DC8C3C681E24AAA559A208");
        

The first GUID listed here (10B7488AC1684253320AE33B03DD5FF5EF50ED2A) is the GUID of the "Start Date" input field; the second (D86D01178C27065D49DC8C3C681E24AAA559A208) is the GUID of the "End Date" input field.

Open the JavaScript editor by clicking " " and selecting "Edit JavaScript for Desktop."

Here is the script:

            /**
 * Calculates the duration/difference between to given date controls
 *
 * @param {String}  [dateControlStart]  GUID of a date control - start date
 * @param {String}  [dateControlEnd]    GUID of a date control - end date
 *
 * @return  {Object}                    Calculated duration in diffent output formats.
 *
 * @example
 * calculateDateDuration("E1CCFCF3E959A0BE9F5D722247C9281013BAF8C5", "339F9A576A1855BE1F072F8B9D09E622BA72CB7C");
 */
function calculateDateDuration(dateControlStart, dateControlEnd) {
    if (!dateControlStart || !dateControlEnd) {
        console.warn("Please provide start/end controls");
        return;
    }

    // datefield guids
    const guids = {
        dateCtrlStart: dateControlStart,
        dateCtrlEnd: dateControlEnd,
    };

    // get the JS date object of the two input fields
    const dateStart = ix.util.getUp(guids.dateCtrlStart).getDateObject();
    const dateEnd = ix.util.getUp(guids.dateCtrlEnd).getDateObject();

    // calculate duration between two fields
    // see: https://momentjs.com/docs/#/displaying/difference/
    const datesDiffInMs = moment(dateEnd).diff(moment(dateStart));

    // now we can format the calculated duration in diffent outputs
    // see: https://momentjs.com/docs/#/durations/
    const duration = {
        // duration in various units
        years: moment.duration(datesDiffInMs).years(),
        months: moment.duration(datesDiffInMs).months(),
        days: moment.duration(datesDiffInMs).days(),
        hours: moment.duration(datesDiffInMs).hours(),
        minutes: moment.duration(datesDiffInMs).minutes(),
        seconds: moment.duration(datesDiffInMs).seconds(),

        // duration in a different unit (instead of milliseconds)
        asYears: moment.duration(datesDiffInMs).asYears(),
        asMonths: moment.duration(datesDiffInMs).asMonths(),
        asDays: moment.duration(datesDiffInMs).asDays(),
        asHours: moment.duration(datesDiffInMs).asHours(),
        asMinutes: moment.duration(datesDiffInMs).asMinutes(),
        asSeconds: moment.duration(datesDiffInMs).asSeconds(),

        // humanized format
        // see: https://momentjs.com/docs/#/durations/humanize/
        humanized: moment.duration(datesDiffInMs).humanize(),
    };

    // print output to console
    console.table(duration);
    return duration;
}