How to restrict date variable to only allow to pick only 4 hours from current time

Vikky_5429
Tera Contributor

How to restrict a planned start date within the next 3 hours on date and time variable on the form 

2 REPLIES 2

AishwaryaShukla
Kilo Guru
Kilo Guru

Hi @Vikky_5429 ,

 

You cannot control the calendar values that come up on the date/time field.
However, you can create an onChange() client script on the date/time field and check if the user has selected a date within 3 hours from now.

If not, you can show a message that time should be within 3 hours and clear out the value (that user selected) at the same time.

In this way, you control the values that can be selected on the field.

You can refer the following article on community on how compare date/time values using a client scrip:
https://www.servicenow.com/community/developer-forum/client-script-date-time-functions/m-p/1457091

Do let me know if you need more help on this.

Thanks,
Aishwarya

bravosa
Kilo Guru

Hi Kumar

You can use the following script

 

var sdt = g_form.getValue('date_to_validae'); //Second Date/Time field
var dttype = 'hour'; //this can be day, hour, minute, second. By default it will return seconds.


var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name', 'getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 4) {
alert('Date/Time need to be at least 4 hours from now.');
}
}