Date/time variable validation selected date should be mamimum within 7 days from start date

raj99918
Tera Contributor

Hi 

 

I have two variables called Start date and End date on catalog form I need a validation on End date, the selected date should be mamimum within 7 days from Start date

Note: Data type for both the variables is Date/Time

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@raj99918 

no scripting required.

You can use Catalog UI policy for this.

AnkurBawiskar_0-1755779337384.png

 

AnkurBawiskar_1-1755779429528.png

 

Output:

AnkurBawiskar_0-1755779802548.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Bhimashankar H
Mega Sage

Hi @raj99918 ,

 

You can write a onChange client script on end date to check the difference between start date and end date it should be maximum 7 days. (or please change in your question. I assumed Maximum difference between start date and end date should be maximum 7 days)

 

Write a below code onChange of endDate.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }
    
    // Get Start Date value
    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');
    
    if (!start || !end) return; // Validate presence
    
    // Convert to Date objects (ServiceNow Date/Time returns ISO 8601 format)
    var startDate = new Date(start);
    var endDate = new Date(end);

    // Calculate difference in days
    var timeDiff = endDate.getTime() - startDate.getTime();
    var dayDiff = timeDiff / (1000 * 3600 * 24);

    if (dayDiff > 7) {
        g_form.showFieldMsg('end_date', 'End date must be within 7 days of Start date.', 'error');
        g_form.setValue('end_date', '');   // clear invalid value
    } else {
        g_form.hideFieldMsg('end_date');   // Hide field message if within range
    }
}

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Abhijit
Tera Expert
refer below script , I used it in flow designer , you should modify as per your neeed
 
var dateField = new GlideDateTime(fd_data._1__extract_supervisor_email.final_date);

var nowTime = new GlideDateTime();

var dur = new GlideDuration();

dur = GlideDateTime.subtract(dateField, nowTime);

var days = dur.getDayPart();

if (days<0) {

var tt = fd_data._1__extract_supervisor_email.final_date + ' 8:40:00';
return tt;
}
else {
 var gt = new GlideDateTime();
    gt.add(21600000);
 return gt.toString();  

}

Ankur Bawiskar
Tera Patron
Tera Patron

@raj99918 

no scripting required.

You can use Catalog UI policy for this.

AnkurBawiskar_0-1755779337384.png

 

AnkurBawiskar_1-1755779429528.png

 

Output:

AnkurBawiskar_0-1755779802548.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Sir jii @Ankur Bawiskar