Restrict end date to be after start date

gnunez
Kilo Guru

Hello all,

I have a script that I got from another thread to restrict the end date to be selected after the start date.

My issue is that it doesn't allow users to select the present date so I want to see if it is possible to exclude past dates but only after the present date.

For example: 

Start Date 12/25/18

End Date 12/25/18 (it should exclude 12/24/18 and anything before that)

 

My Catalog Client Script below:

Type: onChange

function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading)
{
return;
}
//start the validation

if(newValue != '')
{
var ga = new GlideAjax('ConfirmDate');
ga.addParam('sysparm_name', 'chkCurrDate');
ga.addParam('sysparm_date',g_form.getValue('end_vacation'));
ga.getXML(NewParse);
}
function NewParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date selected is a past entry.");
g_form.setValue('end_vacation', ''); // Setting the variable as empty
}}}

8 REPLIES 8

The SN Nerd
Giga Sage
Giga Sage

You need to provide your GlideAjax script, as that is what is doing the calculations.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thanks Paul! Here's the script:

Client callable: checked

Application: Global

var ConfirmDate= Class.create();
ConfirmDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCurrDate : function() {

var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });

Your client script looks good, your script include needs a few tweaks to get this working.

First, I'm wondering if you're missing the 'type' at the bottom of your script. Before you close your script you should have: type: 'ConfirmDate'.

Next, you'll have to create a GlideDate or GlideDateTime record in your script include. I think when it's sent over from the client its stringified so you need to convert it back into a GDT object. I'm also not sure what type gs.now() returns, it's either a string or a GlideDate record. Anyways, here's a script you can try: 

chkCurrDate : function() {

    var input = this.getParameter('sysparm_date');
    var startTime = new GlideDateTime();
    startTime.setDisplayValue(input);
 
    var currDay = new GlideDateTime();
 
    if(startTime < currDay){ 
       return false;
    } else { 
       return true; 
    } 
}

jonmulherin
Giga Expert

I went about this a different way but it will work with what you're trying to do.  First, I created a UI Script.  I've put the description from the script below followed by the code

 

The intent of this script is to do away with the need for basically the same code in multiple client scripts. Five parameters will be passed to the script. The first is the value of the field we'll be comparing to, i.e. Start Date. The second is the field label of the first parameter. The third is the name of the field we are interesting in, i.e End Date. The fourth is the value of TheField. The fifth is the field label of TheField, i.e. End Date.
The script is written for a Date field, not a Date / Time field.

 

function ValidateDateGreaterThanOtherDate(CompareValue, CompareName, TheField, TheValue, TheName) {

var Format = g_user_date_format;

if (CompareValue == '') {
g_form.clearValue(TheField);
g_form.showFieldMsg(TheField, CompareName + ' must be entered before ' + TheName, 'error', true);

}
else {
g_form.hideFieldMsg(TheField);

var CompareValueNum = getDateFromFormat(CompareValue, Format);
var TheValueNum = getDateFromFormat(TheValue, Format);

if (TheValueNum <= CompareValueNum) {
g_form.clearValue(TheField);
g_form.showFieldMsg(TheField, TheName + ' must be later than ' + CompareValue, 'error', true);

}
else
g_form.hideFieldMsg(TheField);
}
}

 

I then call the UI Script from multiple catalog items / record producers where validation is needed to ensure the end date is after the start date.  Since you want the current date to be a valid end date, you just need to replace the "<=" with "<" in the if (TheValueNum <= CompareValueNum) statement above.

 

I call the UI Script in the following manner from catalog client scripts:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var Compare = 'required_date';
var Field = control.name;
var Label = g_form.getLabelOf(Field);
var StartDate = g_form.getValue(Compare);
var CompareName = g_form.getLabelOf(Compare);

ValidateDateGreaterThanOtherDate(StartDate, CompareName, Field, newValue, Label);

}

 

Many ways to tackle it.  Hope this helps someone...