Due Date Field GlideDateTime comparison

Anubhav24
Mega Sage
Mega Sage

Hi All,

 

We have a field Due Date on catalog task form and I have to compare the old and new values on change of due date field in a onChange Client Script.

I am simply getting the value using g_form and then doing the equal to check , do I need to convert these dates or a simple comparison is fine.

 

Thanks in advance.

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Anubhav24 

simply comparing won't work

If you want to compare 2 date/time in client script, take this as example where I am comparing the start and end dates

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

Hi @Ankur Bawiskar ,

 

This piece of code will not work :

  if (oldValue != newValue) {
alert ('date is changed');
}
Currently I have this code and it is working as I expect it to execute.
Please advise accordingly

@Anubhav24 

okay so you want to compare oldValue and newValue

then oldValue != newValue will work and you should be good to use that.

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

Just a discussion point out here @Ankur Bawiskar  , I had this confusion of whether to use getTime or not that is why I created this thread however  if oldValue and newValue will work in comparison will something like you posted in your piece of code for start date and end date , not work unless it is converted to milliseconds using getTime function ? 

Also comparing oldValue and newValue is that a good and recommended practice?