Add days to a date field/ variable

neha_chauhan
Tera Contributor

I am trying to add 28 days to a date variable based on date selected in another variable (i.e. both variables are mapped with form fields and the client script should run on employee service center and the HR case form as well). I have written an onChange() catalog client script and a global scope script include. But as soon as I fill first date variable error comes : 'There is a JavaScript error in your browser console'. Can anyone guide me on what I am doing wrong. PFB the client script and the script include :

Type: OnChange()

Variable: u_date_concern_reported

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28; 
var addtype = 'day'; 

var gr = new GlideAjax('ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateTimeAmountt');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer);  //second date field
alert(answer);
}

}

 

 

Script Include: ClientDateTimeUtils

Application: Global

Client callable:Checked

Accessible from: All application scopes

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype');
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDate();
day.setDisplayValue(firstDT);
day.addDays(addTIME);
return day.getDisplayValue();
},
type: 'ClientDateTimeUtils'
});

1 ACCEPTED SOLUTION

Sufiyan Memon
Kilo Sage
Kilo Sage

Hi neha,

I found some mistakes in your code, I just highlighted that mistake by making those mistakes as Bold. Let me know after correction, are you able to achieve your desire output. If not I can share you some other script as well. 

 

Type: OnChange()

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28; 
var addtype = 'day'; 

var gr = new GlideAjax('global.ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateAmount');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer);  //second date field
alert(answer);
}

}

 

 

Script Include: ClientDateTimeUtils

Application: Global

Client callable:Checked

Accessible from: All application scopes

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype');
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = new GlideDate();
day.setDisplayValue(firstDT);
day.addDays(addTIME);
return day.getDisplayValue();
},
type: 'ClientDateTimeUtils'
});

View solution in original post

10 REPLIES 10

Namrata Khabale
Giga Guru

Hey  neha,

 

 

Refer the Link Below It may help you:

https://community.servicenow.com/community?id=community_question&sys_id=12998ba5db5cdbc01dcaf3231f96...

 

If it works for you, Mark my answer Correct and Helpful.

 

Thanks,

Namrata.

Santosh_Ksagar
Mega Sage
Mega Sage

Hi Neha,

Please find how you can add days:- 

var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addDays(-1);
gs.print(gdt.getDate());

Output:- 

2011-08-30

Please mark answer correct if it helps you.

Thanks

Santosh

Sufiyan Memon
Kilo Sage
Kilo Sage

Hi neha,

I found some mistakes in your code, I just highlighted that mistake by making those mistakes as Bold. Let me know after correction, are you able to achieve your desire output. If not I can share you some other script as well. 

 

Type: OnChange()

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('u_date_concern_reported'); //first date field
var addtime = 28; 
var addtype = 'day'; 

var gr = new GlideAjax('global.ClientDateTimeUtils');
gr.addParam('sysparm_name', 'addDateAmount');
gr.addParam('sysparm_fdt', cdt);
gr.addParam('sysparm_addtime', addtime);
gr.addParam('sysparm_addtype', addtype);
gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse){
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_th_day', answer);  //second date field
alert(answer);
}

}

 

 

Script Include: ClientDateTimeUtils

Application: Global

Client callable:Checked

Accessible from: All application scopes

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype');
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = new GlideDate();
day.setDisplayValue(firstDT);
day.addDays(addTIME);
return day.getDisplayValue();
},
type: 'ClientDateTimeUtils'
});

Hi Sufiyan,

Thanks a lot. Script works now. But I am getting date in a different format in the second field.

28th Day: 2020-03-30

 
Can you please help me on what changes I need to make to get date in input field date format.