Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add 10 days to current date using client scripts

anur
Kilo Contributor

I have a field "RequestedDate" in a service catalog form. Need to populate it with value equal to current date plus 10 days.

Please suggest implementing this with a client script.

7 REPLIES 7

Laxmikanth
Kilo Guru

Hi Anu,



Please refer to the below thread


Client Script Date/Time Functions


raves12
Kilo Sage

Call a script Include and pass the value as parameter to make your calculations server side. Finally get the response object from the script Include and set accordingly.



var strtdt   = gs.nowDateTime() ;


mytime = new GlideDateTime(strtdt);


var gtime1 = new GlideTime();


gtime1.setValue("10:00:00");


mytime.add(gtime1);



You can also refer below post:



https://www.servicenowguru.com/scripting/client-scripts-scripting/client-side-dates-in-servicenow/


anur
Kilo Contributor

I need a client script in onLoad()


gs. scripts not valid in client script.


Can you try sometime like below.



Client callable Script Include:


var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


calDate: function () {


var sdt = new GlideDateTime(this.getParameter('sysparm_date'));


sdt.addDaysLocalTime(10);


return sdt.getDate();


},


type: 'MyDateTimeAjax'


});



Client Script:


var gr = new GlideAjax('MyDateTimeAjax');


gr.addParam('sysparm_name', 'calDate');


gr.addParam('sysparm_date', g_form.getValue('u_date')); //Please update the field name


gr.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


var answer = serverResponse.responseXML.documentElement.getAttribute("answer");


alert(answer);


g_form.setValue('field_name', answer);


}