GlideDate() is working in client Script, however the requirements should be in GlideDateTime (). Is there anyone who can assist me with this?

Ashoka Panchal
Tera Contributor

var recPriority = g_form.getValue('u_rec_rating');
var cdt = new Date();  // When I use GlideDateTime() it's showing error.
var addtime;
var addtype = 'day';
if (recPriority == '2') {

addtime = 7; //The amount of time to add
}
if (recPriority == '3' || recPriority == '4') {

addtime = 90; //The amount of time to add

}

var ajax = new GlideAjax('global.ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'addDateAmount');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_addtime', addtime);
ajax.addParam('sysparm_addtype', addtype);
ajax.getXML(doSomething); //Need callback function

function doSomething(response) {
//Sets field
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('due_date', answer);

Current Result:  find_real_file.png

Expected : DateTime

15 REPLIES 15

Ashoka Panchal
Tera Contributor

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date Field
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = GlideDate();
day.setValue(firstDT);

if(addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}

//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day;
},

 

It's working but, not correct. Need in terms of date/time format

Hi,

Please try below:

addDateAmount: function(){
var firstDT = this.getParameter('sysparm_fdt'); //If you want to add day/time in current date then no need to pass this parameter
var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())
var addTIME = this.getParameter('sysparm_addtime'); //How much time to add
var day = new GlideDateTime();  // this will give you current date and time


if(addTYPE == 'day'){day.addDays(addTIME);}
else if (addTYPE == 'week'){day.addWeeks(addTIME);}
else if (addTYPE == 'month'){day.addMonths(addTIME);}
else if (addTYPE == 'year'){day.addYears(addTIME);}
else {day.addDays(addTIME);}

//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;
return day.getDisplayValue();  // return display value
},

 

Thanks
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

 same issuefind_real_file.png

Vishal Birajdar
Giga Sage

Hello Ashoka,

Please try this solution.

Script Include :

I have hard coded the values of addSeconds(), you can get from your variable.

find_real_file.png

Client Script : 

find_real_file.png

 

This is output I get :

 

find_real_file.png

Hope this will help you

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

find_real_file.png  I am receiving this info message