The CreatorCon Call for Content is officially open! Get started here.

Calculate the difference between start date and end date and populate the difference between them in another field

test1231998
Tera Contributor

I have two fields start_date and end_date.I wanted to writ a client script so that when i select both dates the difference should be populated in another field called no_of_days.

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Deepika,

you will have to write onchange client script on both start_date and end_date fields

Sample script below

Script Include: It should be client callable

var DateCalculations = Class.create();
DateCalculations.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getDifference: function(){

		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));	
        var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));		
		var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
		var days = parseInt(diffSeconds)*60*24;
        return  days; 		
	},
	
    type: 'DateCalculations'
});

Client Script: onChange of Start; similarly you need to have onChange of End

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

if(newValue == ''){
	g_form.clearValue('no_of_days');
}

if(oldValue != newValue){

var ga = new GlideAjax('DateCalculations');
    ga.addParam('sysparm_name', "getDifference");
    ga.addParam('sysparm_startDate', g_form.getValue('start_date'));
	ga.addParam('sysparm_endDate', g_form.getValue('end_date'));
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            g_form.setValue('no_of_days', answer); // give proper field name here
        }
    });
    //Type appropriate comment here, and begin script below
}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Deepika,

you will have to write onchange client script on both start_date and end_date fields

Sample script below

Script Include: It should be client callable

var DateCalculations = Class.create();
DateCalculations.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getDifference: function(){

		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));	
        var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));		
		var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
		var days = parseInt(diffSeconds)*60*24;
        return  days; 		
	},
	
    type: 'DateCalculations'
});

Client Script: onChange of Start; similarly you need to have onChange of End

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

if(newValue == ''){
	g_form.clearValue('no_of_days');
}

if(oldValue != newValue){

var ga = new GlideAjax('DateCalculations');
    ga.addParam('sysparm_name', "getDifference");
    ga.addParam('sysparm_startDate', g_form.getValue('start_date'));
	ga.addParam('sysparm_endDate', g_form.getValue('end_date'));
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            g_form.setValue('no_of_days', answer); // give proper field name here
        }
    });
    //Type appropriate comment here, and begin script below
}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks Ankur,this worked

Hello sir, In onChange client script Sir what type field have you taken?