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

Following script will set the hidden fields based on entered difference of dates between current data and entered entered in field geboortedatum_of_verwachte_datum. onChange is on variable name "geboortedatum_of_verwachte_datum" so I'm using "newValue" which will contain the new value in geboortedatum_of_verwachte_datum.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var startDate = new Date();
        var endDate = new Date(newValue);
        var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = 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);
    }
}

Most of the time, script include is used when it's necessary to use record from the table. When just processing current field values, it's often not required.

Hi Hitosi,

 

Thank you for your reply. I tried your script. It now returns Yes for value from 1-12 december and returns No for any other date. You are definitely on to something here. Now I only to find a way to make it return Yes for every value in the next 26 weeks/182 days from today's date. Any ideas on how to make that work?

I've made field invisible_geboorteverlofdate_check be of type "Yes/No".

Maybe, I'm not understanding the requirement correctly. Is value of geboortedatum_of_verwachte_datum going to after today or before today?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
        var startDate = new Date();
        var endDate = new Date(newValue);
        var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = 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);
    }
}

Execution results:

find_real_file.pngfind_real_file.png

find_real_file.png

If the date may be before today, change the daysDiff statement as follows. This will return Yes on all days when the selected date is within 182 days before or after today and No on all other days.

var daysDiff = Math.abs(Math.floor(timeDiff / (1000 * 3600 * 24)));

I think I found where the problem is. It's the date format. I'm using yyyy-MM-dd in my instance. Probably using some other format which is causing new Date() to error.

If instance's date format is 'dd-MM-yyyy' following code will change the date format.

        endDate = newValue.split("-").reverse().join("-");
        var endDate = new Date(endDate);
        var timeDiff = endDate.getTime() - startDate.getTime();
        var daysDiff = Math.abs(Math.floor(timeDiff / (1000 * 3600 * 24)));