Not able to compare date/time field

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

@Community Alums  

Please add one more condition to the UI policy.

End date is not empty.

Please mark my answer as correct based on Impact


Ankur Bawiskar
Tera Patron

@Community Alums 

you will require 2 onChange 1 for start and 1 for end

Either create 2 onChange or 1 onSubmit to validate

I am sharing onSubmit, you can use similar in onChange

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

	g_form.hideErrorBox('start_date');
	g_form.hideErrorBox('end_date');

	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
		if(end < start){
			var message = 'Please give valid start and end dates';
			g_form.showErrorBox('start_date', message);
			g_form.showErrorBox('end_date', message);
			return false;
		}

	}
}

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

Not applicable

Hi @Ankur Bawiskar ,

I tried the given code, but its not working, when I alert the dates, It shows me Invalid date. Could you please guide me on this.

Thanks!!

@Community Alums 

please share your latest script

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

Not applicable

Hi @Ankur Bawiskar ,

Below is my latest script:

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


	if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
		var start = new Date(g_form.getValue('start_date')).getTime();
		var end = new Date(g_form.getValue('end_date')).getTime();
               alert(start+""+end);    //this gives me invalid date
		if(end < start){
			alert("true");
			return false;
		}

	}
}