How do I populate date field on load two days from current date

Kaz9
Giga Expert

I know you can set the date field to the current date by doing the following

Set Current DateTime In Field - ServiceNow Wiki

Name: Set Current Date/Time In Field

Type: Client Script

Table:

Description: You can use the following two lines to set the current date and time in a date/time field. This bypasses the problem of getting the value into the proper format and proper timezone:

Parameters:

Script: For more information on running Server Side Script with the client please refer to GlideAjax.

var ajax = new GlideAjax('MyDateTimeAjax'); ajax.addParam('sysparm_name', 'nowDateTime'); ajax.getXML(function () { g_form.setValue('put your field name here', ajax.getAnswer()); }); 

System Script Include

// Be sure the "Client callable" checkbox is checked   var MyDateTimeAjax = Class.create(); MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, { nowDateTime: function () { return gs.nowDateTime(); } });


How do I set it to two days from the current date? Any help would be appreciated, thank you.
1 ACCEPTED SOLUTION

MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor,


{ nowDateTime: function ()


{


var nd = new GlideDateTime(gs.nowDateTime());


gs.log('current date is :'+nd);


var fd = new GlideDateTime(nd);


fd.addDays(2);


gs.log('future date will be:'+fd);



return fd;



}


});


View solution in original post

6 REPLIES 6

MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor,


{ nowDateTime: function ()


{


var nd = new GlideDateTime(gs.nowDateTime());


gs.log('current date is :'+nd);


var fd = new GlideDateTime(nd);


fd.addDays(2);


gs.log('future date will be:'+fd);



return fd;



}


});


THANK YOU VERY MUCH!