Set default time on a date/time field

booher04
Tera Guru

Hello - is there an easy way to set a default time on the date/time field variable?  We have a need for the date picker to default to 8:00am EST when you open the calendar to pick the date/time.

1 ACCEPTED SOLUTION

I got it working using below script

function onLoad() {
	var ajax = new GlideAjax('MyDateTimeAjax');
	ajax.addParam('sysparm_name', 'nowDateTime');
	ajax.getXML(function () {
		var test = ajax.getXMLAnswer(function(answer){
			var response = JSON.parse(answer);
			//alert(response.datetime);
			g_form.setValue('date_time_deployment', response.datetime);
		});
	});
}
var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	nowDateTime: function () {
		var Date = new GlideDate(); 
		var datetime = (Date + ' 08:00:00');
		var response = {};
		response.datetime = datetime;
		return JSON.stringify(response);
	},
	type: 'MyDateTimeAjax'
});

View solution in original post

15 REPLIES 15

Ziaur Rahman1
Tera Expert

Hello

 

Please follow below given link, it might help.

 

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

 

Thanks

Ziaur Rahman

Thank you this pointed me in the right direction. The problem I'm having is now I am getting "null 08:00:00" in the field when it loads.  I'd like it to default to the current date and 8:00am, so the time is correct but the date is not working for some reason.

 

Client Script onLoad:

function onLoad() {

//Type appropriate comment here, and begin script below

var ajax = new GlideAjax('MyDateTimeAjax');

ajax.addParam('sysparm_name', 'nowDateTime');

ajax.getXML(function () {

g_form.setValue('date_time_deployment', (ajax.getAnswer() + ' 08:00:00'));

});

}

 

Script Include: Client Callable

var MyDateTimeAjax = Class.create();

MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

nowDateTime: function () {

return gs.now();

}

});

Mike Patel
Tera Sage

Can you check if you have client callable is checked on script includes

Yeah I have that selected:

 

find_real_file.png