H
ave you ever wondered how you can change the default time displayed in a Date/Time dialog? In general, the Date/Time dialog is controlled by back-end code so there’s not much you can do with it. I’ve seen this question a few times now so I decided to come up with a solution.
Because the Date/Time picker dialog is controlled by back-end code, the only way to change the default time is via client scripting. What I found is that you can run a client script when the dialog comes up and set the hours, minutes, and seconds to whatever you want. The trick is running your client script at the right time. You can’t react to a change or a form submission like you might for regular client scripting scenarios. Instead, you need to react to the click of the calendar icon next to the Date/Time field.
Fortunately, the Prototype library that ships with ServiceNow includes something that does exactly what we need…the Event.observe method. The basic idea behind this method is to wait for a specific event (in this case the ‘onClick’ event from the calendar icon) and then run some code along with that event. I’ve used this method before in an article I wrote about controlling the ordering of client-side code.
Event.observe works by attaching itself to a specific event on a specific element. For the purposes of this solution, we need to find the ‘A’ tag associated with the calendar icon (the element) and check for the ‘click’ event. Then we can run the code to set the time to whatever we want…typically midnight or something similar. Below is a function that does just that. It works for regular forms and for catalog items. You just need to call the function and pass in the name of the Date/Time field or variable like this…
…and here’s the ‘setDatePickerTime’ function
//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 = '00';$('GwtDateTimePicker_mm').value = '00';$('GwtDateTimePicker_ss').value = '00';",0);
});
}
Here’s an example client script I set up on the Incident form to run onLoad (which will almost always be the case for this script) and attach event listeners to the ‘Opened’ and ‘Closed’ fields so that their time picker would be set to midnignt.
Name: Set Date Picker Time
Table/Catalog Item/Variable Set: Wherever you need it!
Type: OnLoad
Script:
//Set the date picker default time to '00' for 'Opened' and 'Closed' fields
setDatePickerTime('opened_at');
setDatePickerTime('closed_at');
}
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 = '00';$('GwtDateTimePicker_mm').value = '00';$('GwtDateTimePicker_ss').value = '00';",0);
});
}
This is perfect!!!
Thanks
Great!
this event.observe is really a wonderful thing!
I will test you script right now.
Thank you
it works great, but there is a small problem, if you have already a date time set, the time value is erased by 00:00:00.
I’ll put a if g_form.getValue(‘opened_at’) == “”
or somthing like that to avoid that.
Thanks again for this great script
Hi Mark,
Is there a way i can force the user to select the date only through the date picker and prevent manual entry?
~Mahira
You should be able to do this by using the following in a client script…
The only downsides to this is that there would be no way to clear the field once a date had been populated. Users would also be able to bypass this through a list edit so you’d need to lock down list edit permissions for the field using an ACL as well.
Hi Mark,
Even i have the similar problem.like not to allow the user to manually enter date-time.
but when I tried using the script g_form.getControl(‘start_date’).disabled=’true’; to my surprise the data in date/time field is not being populating once i save it.
You’re right. You should use ‘readOnly’ in place of ‘disabled’ to avoid this issue. I’ve changed my comment from ‘disabled’ to ‘readOnly’ as well to avoid confusion.
Hi Mark,
Where should I get the all elements like GwtDateTimePicker_hh of calender?
There’s not a listing of all of the elements. You can use a dom inspector (like firebug) to get the element IDs though.
Thank you Mark,
Hope ,this will help me to shade only freeze dates in Calender…
Hi mark,
I want to shade only the freeze dates in calender while picking dates for a task in a change.
Can you tell me How to compare all the calender dates with freeze schedule dates…?
I’m not aware of a way to do that off-hand. You might try asking on the forums.
We have recently upgraded to the Berlin CA and we’ve been going through testing functionality. Have you noticed that the Berlin CA breaks the functionality of this script? Have you found a work-around? I’ll post more info as I diagnose it.
Thanks
If you’ve got your hands on a Berlin build then you’ve seen more of it than I have. Have you tried this out on a ServiceNow demo instance?
Just tried it on the Demo site and had the same problem. The javascript error:
Error: TypeError: $(“GwtDateTimePicker_hh”) is null
Source File: https://demo12.service-now.com/change_request.do?sys_id=a9e9c33dc61122760072455df62663d2&sysparm_from_related_list=false&sysparm_record_list=active%3dtrue%5eORDERBYnumber&sysparm_record_row=1&sysparm_record_rows=17&sysparm_record_target=change_request&sysparm_view=routineChange&sysparm_view_forced=true
Line: 860
Looks like a timing issue with the client script. Try this as your ‘setDatePickerTime’ function instead…
//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 = '00';$('GwtDateTimePicker_mm').value = '00';$('GwtDateTimePicker_ss').value = '00';",0);
});
}
We have tried this as a fix to the Berlin timing issue on our instance, but are still running into java errors.
I have added your example to the demo11 instance and receive errors there as well.
@Steve, I think it’s working correctly. Are you sure you’re using the correct field names? The script on demo11 doesn’t look like it’s using valid field names.
Sorry, you may be looking at the wrong entry… I used your example above on the incident table. The one on the change is specific to our instance.
Here is the script I added to the demo site.
https://demo11.service-now.com/nav_to.do?uri=sys_script_client.do?sys_id=adf34adeb02030005c777f3b565ef97d%26sysparm_view=portal
I copied your script above and placed on the incident table. I doubled checked both field names on the form (opened_at and closed_at) just to be sure the demo and the script were the same.
Okay, I found the issue (which was specific to IE only in my testing). I’ve updated the script above and it’s working in demo11. Give it a try and let me know how it goes.
Thanks Mark!!
That was our issue as we are predominately an IE shop.
It also appears that our sandbox instance must have an error as your fix did not work at all, but as a last resort, I went ahead and updated the script in our Dev instance and it works like a charm. Thanks for following up when you didn’t have to…
Very nice for setting the hours, minutes and seconds. Any thoughts on similar setting the date – I’ve been asked to arrange, on the Change form, for the Planned End Date to default to the same date as Planned Start Date. Peering darkly into the client code, the issue seems to be that it creates a GwtDateTimePicker object, but tragically throws away the reference 🙁
Any ideas, short of just copying Planned Start Date across to Planned End Date in an onChange client script, which is not terribly nice.
Nothing else comes to mind at the moment unfortunately. I don’t see any obvious hooks for doing that.
Hi. I used this script for a number of date/time fields on our Change form, but I am experiencing a strange issue:
I have an additional client script that is highlighting conditionally mandatory fields (ridiculous? yes – but necessary), and when I activated the script for the date/time picker, that script stopped working … for everyone except admins. I have absolutely zero idea why that would be, but when i turn off the date/time picker script, my field highlights work again.
Any thoughts?
script as written:
function onLoad() {
//Set the date picker default time to ’00’ for ‘Opened’ and ‘Closed’ fields
setDatePickerTime(‘start_date’);
setDatePickerTime(‘end_date’);
setDatePickerTime(‘u_target_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 = ’00’;$(‘GwtDateTimePicker_mm’).value = ’00’;$(‘GwtDateTimePicker_ss’).value = ’00’;”,0);
});
}
additional notes:
we are at Berlin release – I’ve seen other reports of this not working on the SN forum, but it worked just fine when it was active – set zeros for HH MM and SS values.
I don’t think it has anything to do with the Berlin release if it’s working fine in absence of the other script. My guess is that there’s some sort of timing or conflict issue between the two. You might try putting the code inside your ‘onLoad’ script inside of a try/catch block to see if that helps.
This is months late, but I’ve just been debugging an issue of precisely this kind.
The key difference between admin and non-admin users, in my case, was that the admin users had write access to the date fields and the non-admin ones didn’t. That meant the calendar picker icon and its surrounding link element, which normally appears immediately after the HTML input element, wasn’t there for the non-admins.
This is correct behaviour, but the setDatePickerTime function was failing in this case (where the field was read-only and the link absent) as it just assumed that the next(a) call would return an element.
My quick fix for the function just adds a check for the existence of the hyperlink:
function setDatePickerTime(field){
//Attach an event listener to set default time value for date picker
var linkElement = $(g_form.getControl(field).id).next(‘a’);
if (linkElement !== undefined) {
Event.observe(linkElement, ‘click’, function() {
setTimeout(“$(‘GwtDateTimePicker_hh’).value = ’00’;$(‘GwtDateTimePicker_mm’).value = ’00’;$(‘GwtDateTimePicker_ss’).value = ’00’;”,0);
});
}
}
We have a problem with the translation of the days in the datepicker. Any idea what the field names are for the one letter week day codes ?
As far as I know those aren’t translatable. You’ll probably need to contact ServiceNow support to request an enhancement.
Brilliant Mark!
Hi Mark,
That script works great! Thanks for posting that. I’m using a 12 hour format so I need to set the time to 3:00 pm. It’s setting to 3:00, but am. Is there a way to set it to pm? Thanks again for your help.
Sure. That’s just another element. You should be able to add this line to your script to set that.
$(‘GwtDateTimePicker_ampm’).value = ‘PM’;
I implemented this script last year in Calgary, and it has been working great. However, in Fuji it does not appear to work at all. Any suggestions?
Thanks!
I’ve looked at this in Fuji and I’m not sure there’s a fix unfortunately. This script depends on being able to listen to the ‘click’ event on that icon. It attaches the listener correctly, but ServiceNow doesn’t seem to be triggering the ‘click’ event on those icons in the same way. You might just have to ask ServiceNow if they have another way to set the default picker time for a particular field.
Had a look into this and looks like the UI renders fractionally slower in Fuji than in Eureka, so the timeout 0 was failing. Also, the DOM has changed so it could not find the element. So changed the setting function to try repeatedly until it finds the element, and also updated the DOM selector to find the element. Seems to do the trick:
setDatePickerTime(‘dateField’);
function setDatePickerTime(field){
//Attach an event listener to set default time value for date picker
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 = ’01’;
$(‘GwtDateTimePicker_mm’).value = ’02’;
$(‘GwtDateTimePicker_ss’).value = ’03’;
}
}
}
Mark,
Have you seen this post?
Shannon seems to have found a workaround for Fuji.
On checking the syntax, I get the “WARNING Implied eval is evil. Pass a function instead of a string.” error on the setTimeout line, but I also have that error on Eureka, so I am not sure it means much.
I have tried multiple versions of this script with no success. Has anyone been able to get this script working in Fuji or have another alternative to defaulting the time of the dateTimePicker?
We are skipping Fuji and going directly from Eureka to Geneva and just discovered it is not working there either. Anybody have any luck on an alternative yet?
Hi Cynthia,
We are on Geneva and I just tried the solution given by Ahmed Hmeid on 23-07-2015, 06:12 (3 posts above yours).
It works perfectly!