- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 06-17-2021 09:47 PM
Hi Community,
The use case is to restrict past dates in the 'Date' field. And it needs to be validated at the form level and also at the list view.
Note: Select the date field that the client script runs on.
OnChange:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cdt = newValue; //value for the Desired Due Date on the form
var dttype = 'day'; //this can be day, hour, minute, second.
var ajax = new GlideAjax('ClientDateTimeUtils'); //ajax call to compare selected date
ajax.addParam('sysparm_name', 'getNowDateTimeDiff'); //function call to do the calculation
ajax.addParam('sysparm_fdt', cdt); //pass the date
ajax.addParam('sysparm_difftype', dttype); //pass the format
ajax.getXML(doSomething); //callback function to present the results
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if (answer < -1) {
g_form.clearValue('u_installation_date'); //clear the invalid value
alert('Please select today or a date in the future.'); //alert user to re-enter
}
}
}
OnCellEdit:
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
//Type appropriate comment here, and begin script below
var cdt = newValue; //value for the Desired Due Date on the form
var dttype = 'day'; //this can be day, hour, minute, second.
var ajax = new GlideAjax('ClientDateTimeUtils'); //ajax call to compare selected date
ajax.addParam('sysparm_name', 'getNowDateTimeDiff'); //function call to do the calculation
ajax.addParam('sysparm_fdt', cdt); //pass the date
ajax.addParam('sysparm_difftype', dttype); //pass the format
ajax.getXML(doSomething); //callback function to present the results
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < -1) {
alert('Please select today or a date in the future.'); //alert user to re-enter
saveAndClose = false;
}
callback(saveAndClose);
}
}
Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Takes a Single Date/Time Field and returns its time difference from nowDateTime().
//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
return timediff;
}
});

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
Restricting dates can also be done almost no code (only code would be the error message), using (Catalog) UI Policy. Is that something to consider first, instead of client scripting?
Kind regards,
Mark
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Yes,
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi can you please suggest me if i want to restrict the past tie of current date how can i achieve it?
Suppose i am selecting the time as 11:00 a.m but the actual time of the system is 12:00p.m then also my date should not get selected. How can i achieve it?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi @anayna ,
We can do this using date diff function which will give us the difference in seconds and we need to compare them, if greater than or equal to zero accept it and if less than zero give an error msg.
Thanks,
Shri.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
No Code Solution for not allowing past dates with a Business Rule > this works for both the Form View and the List View: