Regarding date/time variable.

Southsayer
Tera Expert

Hi everyone, 

 

I need to create a date/time type variable.

And in the date picker, the date should be only future dates and allow only upto 90 days. 

 

Thanks in advance for your help! 

1 ACCEPTED SOLUTION

Hi @Southsayer ,

Then you can use the 2 onchange client scripts.

1. Create onchange client script on start date and use below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (start > end) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

2. Create other onchange client script on end date and use below script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
        var start = new Date(g_form.getValue('start_date')); //your start date variable name
        var end = new Date(g_form.getValue('end_date')); //your end date variable name
        if (end < start) {
            g_form.addErrorMessage('Verify start date and end date');
            return false;
        }
    }
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

11 REPLIES 11

younes_sebti
Kilo Sage

The only way today is to create a catalog client script onChange that will do a asynchronous call to a script include


In this example the script include simply return gs.nowDateTime();

var ajax = new GlideAjax('your script include');
ajax.addParam('sysparm_name','your function');
ajax.getXML(doSomething);

function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer > newValue) {
g_form.clearValue("your field");
g_form.showFieldMsg("field", "Date provided should not be in the past", "error");
return;
}

// and another function to verify the datediff is lower to 90 days
}

Pavankumar_1
Mega Patron

Hi @Southsayer ,

To achieve this requirement you can create UI policy with the conditions as shown below.

1. Select your date and time variable on UI policy and use below conditions.

Screenshot (263).png

2. you can select only future dates 90 days from now.

Script:

 

function onCondition() {
    alert("Enter valid date, You can select future date upto 90 days from today");
    g_form.clearValue('start_date');
}

 

Screenshot (260).png

3.Below is the alert you can see when the date is after 90 days from now and same for when you select past dates.

Screenshot (261).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi Pavan, 

I want to not show any kind of alerts, just the calendar should only show the above mention dates, could you please help out?

 

Hi @Southsayer ,

There is no method for the date variable to make the past/future dates read-only or greyed out.

Only we can do is the validation.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar