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

Hi @Southsayer ,

No you need to use Onchange Client scripts only for this. Try below it will work.

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

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var endDate = new Date(g_form.getValue('end_date'));
    var startDate = new Date(newValue);
    if (startDate >= endDate) {
        alert('start date must be before end date');
        g_form.setValue('start_date', '');
    }
}

 

 

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

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var startDate = new Date(g_form.getValue('start_date'));
    var endDate = new Date(newValue); 
    if (endDate <= startDate) {
        alert('End date must be after start date');
        g_form.setValue('end_date', '');
    }
}

 

 

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

Pavankumar_1
Mega Patron

Hi @Southsayer ,

Hope your issue got resolved close the question by Accepting solution and hit thumb icon.

Once you are issue got resolved please Accept solution and Close then only others will refer and get benefited.

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