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

Sai Kumar B
Mega Sage
Mega Sage

Hi @Coen Eskens 

Can you try with the below modified script include?

Also try adding info messages b/w lines in client script to debug

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';

        }

    }

});

Hi Sai,

 

Unfortunately the outcome is still the same after trying this.

Hitoshi Ozawa
Giga Sage
Giga Sage

Any reason to be using Script Include to calculate date difference? 

Following script will calculate number of days between 2 days. The script is using "start_date" and "end_date" as fields.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var startDate = new Date(g_form.getValue('start_date'));
        var endDate = new Date(newValue);
		var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = timeDiff / (1000 * 3600 * 24);
        alert(daysDiff);
    } catch (err) {
        alert(err.message);
    }
}