Script Not taking year into calculation

TylerJ91
Kilo Contributor

Hello,  

Currently i have a Client script listed below that is supposed to prevent the end date from being before the start date value. this script does work IF its the same year. if you put the end date to 2025 and the start date to 2026 it does not work. but if its the same year it works fine. Any help as to why it does not account for the year or work when a previous year is selected for the end date? 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var startDate = g_form.getValue('u_start_date');
    var endDate = g_form.getValue('u_end_date');
if (endDate <= startDate) {
	g_form.addErrorMessage('Expected Completion Date cannot be before Expected Start Date');
	return false;
}
return true;
}




2 ACCEPTED SOLUTIONS

KanteS
Giga Guru

Hi 

The issue occurs because g_form.getValue() returns the date as a string, and your comparison is happening as a string comparison, not as a date comparison. 

To properly compare dates, you need to convert the values into actual date objects before comparing them.

 

Revised Client Script: please try the below one,

 

function onSubmit() {

 

var startDateStr = g_form.getValue('u_start_date');

var endDateStr = g_form.getValue('u_end_date');

 

if (!startDateStr || !endDateStr) {

return true;

}

 

var startDate = new Date(startDateStr);

var endDate = new Date(endDateStr);

 

if (endDate < startDate) {

g_form.addErrorMessage('Expected Completion Date cannot be before Expected Start Date');

return false;

}

return true;

}
If this resolved your issue, please mark this response as the correct answer and give it a like

View solution in original post

Ankur Bawiskar
Tera Patron

@TylerJ91 

update as this

-> you should not compare those values as those are string

-> convert that to time and then compare

function onSubmit() {
    //Type appropriate comment here, and begin script below

    if (g_form.getValue('u_start_date') != '' && g_form.getValue('u_end_date')) {
        var start = new Date(g_form.getValue('u_start_date')).getTime();
        var end = new Date(g_form.getValue('u_end_date')).getTime();
        if (end < start) {
            g_form.addErrorMessage('Expected Completion Date cannot be before Expected Start Date');
            return false;
        }

    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

3 REPLIES 3

KanteS
Giga Guru

Hi 

The issue occurs because g_form.getValue() returns the date as a string, and your comparison is happening as a string comparison, not as a date comparison. 

To properly compare dates, you need to convert the values into actual date objects before comparing them.

 

Revised Client Script: please try the below one,

 

function onSubmit() {

 

var startDateStr = g_form.getValue('u_start_date');

var endDateStr = g_form.getValue('u_end_date');

 

if (!startDateStr || !endDateStr) {

return true;

}

 

var startDate = new Date(startDateStr);

var endDate = new Date(endDateStr);

 

if (endDate < startDate) {

g_form.addErrorMessage('Expected Completion Date cannot be before Expected Start Date');

return false;

}

return true;

}
If this resolved your issue, please mark this response as the correct answer and give it a like

Anurag Tripathi
Mega Patron

If you have to do this in client script I'll suggest use GlideAjax like below

Comparing Client Dates - ServiceNow Community

 

Alternatively try and use this no code solution using UI policies

AnuragTripathi_0-1768398710475.png

 

In the script part you can add what to do if the condition is true and what to do when it is false.

 

 

-Anurag

Ankur Bawiskar
Tera Patron

@TylerJ91 

update as this

-> you should not compare those values as those are string

-> convert that to time and then compare

function onSubmit() {
    //Type appropriate comment here, and begin script below

    if (g_form.getValue('u_start_date') != '' && g_form.getValue('u_end_date')) {
        var start = new Date(g_form.getValue('u_start_date')).getTime();
        var end = new Date(g_form.getValue('u_end_date')).getTime();
        if (end < start) {
            g_form.addErrorMessage('Expected Completion Date cannot be before Expected Start Date');
            return false;
        }

    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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