Data/Time Field(DatePicker)

kelango87
Kilo Explorer

Hi All,

Actually my requirement is to hide the past date in date/time field in service-now.This functionality is avaliable in jQuery,but i dont know how to implement in javascript\Service-Now.

Note:
In Date/Time field in service-now, the user can select only current or future date and past date should be disable.

6 REPLIES 6

zachkenyon
ServiceNow Employee
ServiceNow Employee

You can do this with an onChange client script, here's how I implemented it for one of my catalog items for the end_date field:



function onChange(control, oldValue, newValue, isLoading) {

if(isLoading || newValue == '') {
return;
}
var date = new Date();
var ed= new Date(getDateFromFormat(g_form.getValue('end_date'),g_user_date_time_format));

if (ed < date) {
alert('End date must be in the future.');
g_form.clearValue('end_date');
}

}



This would be for a GlideDateTime field, to do the same thing on a GlideDate field you would use g_user_date_format instead of g_user_date_time_format above.


ravali6
Giga Contributor

I am sure this will serve the purpose of preventing user from entering past dates but it will not disable the past dates I guess. I also implemented something like this just to fix the problem but would love to know if there is any way we can hide the past dates.


geoffcox
Giga Guru

It is possible to develop your own date gadget if you need to.
Make the existing GlideDate Field read only.
Then add a new field next to it using your own gadget.

What you will need is a UI Macro to define your date gadget, then a formatter to make your UI Macro available as a thing you can add to your form. The UI Macro should include the code to copy the date value from your gadget to the actual GlideDate Field.

The UI Macro can take advantage of jQuery as well.

Note that UI Macros are written in XML, so be careful of your code; certain symbols need to be escaped to avoid looking like tags to the XML interpreter.



<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g2:evaluate jelly="true" var="jvar_myWidget" >

var return_val = '';
var the_current_date = current.u_your_real_glideDate_field;
return_val = some_html_that_makes_your_calendar_widget(the_current_date)
return_val;

</g2:evaluate>

$[jvar_myWidget]

</j:jelly>


The you can create a UI Formatter (System UI -> Formatters) to associate your UI Macro with your table. Then you can customize your form to add the formatter to it next to your original (now read only) glideDate field.

Hope this helps!

Cheers,
Geoff.


ashish68
Kilo Expert

You can achieve the same by writing the following in two ways:



1. UI Script - you can write a UI script which picks the Field type of data time and then you can verify the same if it is less than the current data set the value of the DOM of field to null. The script needs to be called in a function which needs to be called inside addLoadEvent(){


funiton();   // this will do your scripting.


}



2. Use Validation Scripts- These are for global validation, and can be triggered to work @ your end.





Regards,


Ashish