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 set current Date and time

Jishnu C Gopal
Kilo Contributor

How to set Date Field to Current date using UI Action

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

you need something like for date/time for date only use gs.now() instead of gs.nowDateTime()

UI Action

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

Script Includes

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

As already pasted the link please use this method.

View solution in original post

2 REPLIES 2

Mike Patel
Tera Sage

you need something like for date/time for date only use gs.now() instead of gs.nowDateTime()

UI Action

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

Script Includes

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

As already pasted the link please use this method.

bernyalvarado
Mega Sage

It depends on the UI action type. For a server side type of UI action you can simply do:

var gdt = new GlideDateTime();
current.myDateFieldName = gdt
.getDate();

You will need to replace myDateFieldName with the actual field you want to assign the date to 🙂

Thanks,
Berny