The CreatorCon Call for Content is officially open! Get started here.

Set a default time in Date/Time calendar popup

Marcel H_
Tera Guru

I have a request from a team that wants to have the Due Date field on their custom table (extended from Task) have a default time set to 04:30:00PM whenever they open the date picker calendar popup for that field. I've read through quite a few posts here in the Community that provide solutions, many of which are shown marked as correct, but so far none of the methods have worked for me. So far I've tried the following:

onLoad Client Script on the custom table:

function onLoad() {
	var field = g_form.getControl('due_date');

if(field){
	Event.observe($(field.id).up(1).down('a'), 'click', function() {
	setTimeout(setDefaultTime,300); // Some time delay added
});     }

function setDefaultTime() {
$('GwtDateTimePicker_hh').value = '16';
$('GwtDateTimePicker_mm').value = '30';
$('GwtDateTimePicker_ss').value = '00';

}
}

onLoad Client Script on the custom table 2:

function onLoad() {

var field = g_form.getControl('due_date');
alert(field);
alert($(field.id).next('a'));

if(field){
Event.observe($(field.id).next('a'), 'click', function() {
setTimeout(setDefaultTime,300); // Some time delay added
}); }

function setDefaultTime() {
$('GwtDateTimePicker_hh').value = '16';
$('GwtDateTimePicker_mm').value = '30';
$('GwtDateTimePicker_ss').value = '00';

}
}

onLoad Client Script on the custom table 3:

function onLoad() {
   setDatePickerTime('due_date');
}

function setDatePickerTime(field){
   //Attach an event listener to set default time value for date picker
   Event.observe($(g_form.getControl(field).id).next('a'), 'click', function() {
   setTimeout("$('GwtDateTimePicker_hh').value = '16';$('GwtDateTimePicker_mm').value = '30';$('GwtDateTimePicker_ss').value = '00';",0);
   });
}

onLoad Client Script on the custom table 4, with Script Include call:

function onLoad() {
	
	var ajax = new GlideAjax('MyDateTimeAjax'); //Make call to script include
	ajax.addParam('sysparm_name', 'nowDateTime');
	ajax.getXML(function () {
		var test = ajax.getXMLAnswer(function(answer){
			var response = JSON.parse(answer);
			g_form.setValue('due_date', response.datetime); //Set value of Due Date with response from script include
		});
	});
}
var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

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

 

So far none of these have worked, when I open the data picker, it still defaults to the current date/time, or if I use the Client Script/Script Include method is sets the field value onLoad, but I just want the date/time picker to have the default time set.

I also tried doing a Dictionary Override on for the Due Date field on the custom table and set the default value to javascript:gs.now() + " 16:30:00 ";  but that also doesn't change the default time displayed.

Any help on getting this to work as expected would be great. Thanks!

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Marcel,

use this and it would work; I have test this

javascript: new GlideDateTime(new GlideDate() + ' 21:30:00')

the reason I have given 21:30:00 is because my time zone is central and there is a difference of 5 hours between GMT and central time;

the above will show the time in user's time zone i.e. 16:30:00 for users with central time zone

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Marcel H_
Tera Guru

Thanks Ankur, this does work well to set the default date/time on a new record, but I am looking to just set the default time in the date/time picker popup window for the field, not set the default value of the field itself since frequently there won't be a due date set at all, but when one is set the time should default to 16:30:00 of the user's time zone like the picture below

find_real_file.png 

Sandeep127
Tera Contributor

Here is the solution for date/time picker

 

function onLoad()


{


setDatePickerTime('u_last_scan_date');

 

}


function setDatePickerTime(field)


{


Event.observe($(g_form.getControl(field).id).next().down(), 'click', function()


{


setTimeout(setDefaultTime,0);


});


}


function setDefaultTime() {


if (!$('GwtDateTimePicker_hh')) {


setTimeout(setDefaultTime,0);


}


else


{


$('GwtDateTimePicker_hh').value = '00';


$('GwtDateTimePicker_mm').value = '00';


$('GwtDateTimePicker_ss').value = '00';


document.getElementById("GwtDateTimePicker_hh").readOnly = true;


document.getElementById("GwtDateTimePicker_mm").readOnly = true;


document.getElementById("GwtDateTimePicker_ss").readOnly = true;


}


}

Hello Sandeep,

I've tried to use your script but nothing happen.

Where can I maybe wrong ? I've changed the name of the field with one of mine
 
Thanks a lot,
 
Michaël