Restrict the end date field not to select more than 365 days from the start date

Community Alums
Not applicable

Hi All, 

 

Having a requirement that, 

 

need to restrict the end date field not to select the date range more than 365 days from the start date. 

Need assistance on the onchange script to get this sorted. 

 

 

7 REPLIES 7

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Community Alums Try below onChange Client Script on End Date field

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

    var startDate = g_form.getValue('start_date');
    var endDate = g_form.getValue('end_date');

    if (startDate && endDate) {
        var startObj = new GlideDateTime(startDate);
        var endObj = new GlideDateTime(endDate);

        var difference = gs.dateDiff(startObj, endObj, true);

        if (difference > 365) {
            alert('End date cannot be more than 365 days from the start date.');
            g_form.setValue('end_date', ''); 
        }
    }
}

 

Regards,

Sid

 

Trupti94
Tera Guru

Hi @Community Alums ,

you can achieve it using onchange of client script,


var startDateField = 'start_date'; // Replace with your actual start date field name
var endDateField = 'end_date'; // Replace with your actual end date field name

// Function to calculate maximum allowed end date
function calculateMaxEndDate(startDate) {
var maxEndDate = new GlideDateTime(startDate);
maxEndDate.addDays(365);
return maxEndDate;
}

// Function to validate end date
function validateEndDate() {
var startDate = g_form.getValue(startDateField);
var endDate = g_form.getValue(endDateField);

if (startDate && endDate) {
var maxEndDate = calculateMaxEndDate(startDate);
var endDateTime = new GlideDateTime(endDate);

if (endDateTime.compareTo(maxEndDate) > 0) {
alert('End date must be within 365 days from the start date.');
g_form.setValue(endDateField, ''); // Clear the end date field
}
}
}

})();

Robbie
Kilo Patron
Kilo Patron

Hi @Community Alums,

 

This is very easily achieved with little to no code by leveraging a UI Policy as shown in below screen shots.

To create a UI policy, right click on the top of the form you wish to apply it - eg Incident form, and go to 'Configure' > 'Ui Policies'

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

Thanks, Robbie

 

Screenshot 2024-07-25 at 09.45.43.pngScreenshot 2024-07-25 at 09.46.33.png

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Community Alums 

You can refer below post which has similar requirement

Instead of gdt.addMonths(2) you can use gdt.addDays(365)

https://www.servicenow.com/community/developer-forum/end-date-should-not-be-more-than-6-months-from-start-date/m-p/1751634

 

or you can use No-code UI policy

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-policies/ta-p/2297600

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP