Not able to compare date/time field

Community Alums
Not applicable

Hi All,

I have to field start date and end date. I want to put validation so that end date should not be before start date. I am able to achieve this but it is getting failed in one case.

case:

Start date :31/10/2023

End date: 2/11/2023

In above case, code consider end date as less than start date.

 

 

 

Client script
function onChange(control, oldValue, newValue, isLoading) {



if (isLoading || newValue == '') {



return;



}





var ga = new GlideAjax('DateTimeFunc');



ga.addParam('sysparm_name', 'compareDates');



ga.addParam('start_date', g_form.getValue('date_time_from'));



ga.addParam('end_date', g_form.getValue('date_time_to'));



ga.getXML(getEndDate);



}



function getEndDate(response) {



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



if (answer=='true')
{
alert("End date should be after start date");
  //g_form.clearValue('date_time_to');
}
}

Script includes

compareDates: function() {



var chg_start_date = this.getParameter('start_date');



var chg_end_date = this.getParameter('end_date');



if (chg_start_date > chg_end_date)



{



return true;



}



return false;



},

 

 

 

10 REPLIES 10

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Community Alums ,

 

Please try the below code: OnChange script on Planned End Date field

 

Note : Pls use proper backend values in the below script

 

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

//Type appropriate comment here, and begin script below
var startDate = new Date(g_form.getValue('start_date')); //get the value of the start_date field using the g_form.getValue method
var checkEnd = new Date(newValue); // create a new JS date from the desired end_date field (using newValue parameter)

if (checkEnd <= startDate){ //Again - a simple JS Date object comparison
alert('End date must be after start date');
g_form.setValue('end_date','');
}
}

 

 

Thanks,

Danish 

 

Community Alums
Not applicable

Hi @Danish Bhairag2 ,

I tried your code, but its not working. when I alert the dates to troubleshoot its saying invalid dates. Could you please help me on this.

Thanks!!

swathisarang98
Giga Sage
Giga Sage

Hi @Community Alums ,

 

You can create a UI policy with the condition as below,

End date "Is more than"  0 Hours "before" start date  and in the script execute if true you can write below code 

 

end date validation.PNG

Script: 

 

 

function onCondition() {
alert('End Date should not be before Start Date');
g_form.clearValue('end_date_time');
}

 

 

end date validation script.PNG

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,

Swathi Sarang

 

Community Alums
Not applicable

Hi @swathisarang98 ,

I tried the method which you have mentioned, however, when the end date is empty still its saying end date should not be before start date. Could you please help me on  this.

Thanks!!