Issue with calculating date difference between two date fields.

Coen Eskens
Tera Contributor

Hello,

I am running into an issue when trying to calculate the difference in days between two date fields on a record producer. I am using a catalog client script that calls a script include that uses a gs.dateDiff function.

When a user picks a date in the date field called “geboortedatum_of_verwachte_datum” I want the on change catalog client script to call the Script Include through GlideAjax to calculate if the picked date is less or more than 26 weeks from now. The script include should return true if the picked date is less than 26 weeks from now and return false if more than 26 weeks from now.

The on change client script should then set another field called 'invisible_geboorteverlofdate_check' to “Yes” if answer is true or “no” if the answer is false.

My issue is that the script include always seems to return true and the client script always sets the value of “yes” to the 'invisible_geboorteverlofdate_check' field.

I have no prior experience with gs.dateDiff and I wonder what I need to change in order for this to work.

------------------------------------------------------------------------------

The Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {

    if (newValue) {

        var date1 = g_form.getValue(geboortedatum_of_verwachte_datum);

        var today = new Date();

        today.setDate(today.getDate());

 

        var ga = new GlideAjax('SvcCatalogCheckEndDate'); //Name of the Script Include

        ga.addParam('sysparm_name', 'chkCatEndDate'); //Name of the function in the script include

        ga.addParam('sysparm_date', date1); //Parameter to pass to the script include

        ga.addParam('sysparm_endDate', today); //Parameter to pass to the script include

        ga.getXML(SvcCatalogCheckEndDateParse);

    }

}

 

//Function that gets the response and will return to the client. You can place your alert in this function

function SvcCatalogCheckEndDateParse(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer == 'true') {

        g_form.setValue('invisible_geboorteverlofdate_check', 'Yes');

    } else {

        g_form.setValue('invisible_geboorteverlofdate_check', 'No');

    }

}

------------------------------------------------------------------------------

The Script Include:

var SvcCatalogCheckEndDate = Class.create();

 

SvcCatalogCheckEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    chkCatEndDate: function() {

        var start = this.getParameter('sysparm_date'); //Passing the start date from the client

        var end = this.getParameter('sysparm_endDate'); //Passing the end date from the client

        var dif = gs.dateDiff(start, end, true); //Get the Difference between dates.

        if (dif <= 182) {

            return true;

        } else {

            return false;

        }

    }

});

 

-------------------------------------------------------------------------------

The dif <= 182 represents the 26 weeks as 26*7 = 182.

 

I hope you can guide me in the right direction.

1 ACCEPTED SOLUTION

@Coen Eskens 

The problem may be with the date format. Need to change the date format to the format Date() can handle. ServiceNow has a function getDateFromFormat() to do just that. I've changed my script accordingly.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var startDate = new Date();
		var endDate = getDateFromFormat(newValue, g_user_date_format); 
		endDate = new Date(endDate);
        var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = Math.abs(Math.floor(timeDiff / (1000 * 3600 * 24)));
        if (daysDiff < 182) {
            g_form.setValue('invisible_geboorteverlofdate_check', 'Yes');
        } else {
            g_form.setValue('invisible_geboorteverlofdate_check', 'No');
        }
    } catch (err) {
        alert(err.message);
    }
}

View solution in original post

14 REPLIES 14

@Coen Eskens 

The problem may be with the date format. Need to change the date format to the format Date() can handle. ServiceNow has a function getDateFromFormat() to do just that. I've changed my script accordingly.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var startDate = new Date();
		var endDate = getDateFromFormat(newValue, g_user_date_format); 
		endDate = new Date(endDate);
        var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = Math.abs(Math.floor(timeDiff / (1000 * 3600 * 24)));
        if (daysDiff < 182) {
            g_form.setValue('invisible_geboorteverlofdate_check', 'Yes');
        } else {
            g_form.setValue('invisible_geboorteverlofdate_check', 'No');
        }
    } catch (err) {
        alert(err.message);
    }
}

Thank you very much for all your help Hitosi,

This solved my issue. Thanks for the explanation as well, really helpful.

Hi Hitosi,

 

If everything can be done with a client script alone that would be great.

I have always used script includes for time calculations and don't really know of a way to let a client script calculate a day difference between two dates and set a value to another field based on the outcome without a script include. 

I've provided a sample above. If there's any problem with the script, let me know.

charley_egmose
Kilo Explorer

Hi Coen

Your problem seems to be a mix of different datetypes and methods.

Have a look at this from support: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0594663

And this, describing the javascript date methods, getDate() returns an integer in the range 1-31.

Regards