Validate catalog variable of date/time type

wiltonr
Giga Contributor

I have a catalog item that has a date variable for "Date of Event".   Then I have three date/time variables for Set Up Time, Start Time and End Time.   I need a way to a) validate that the date/time field is entered correctly and b) make sure that the Start Time date portion of the date/time variable is the same date as the "Date of Event" variable.   Currently I am able to enter any string into the date/time variable.

So if the Date of Event is 09-20-2016, then I need to validate that the Start Time date can only be 09-20-2016.   Does anyone have a script for this?

Thanks,

Rhonda

1 ACCEPTED SOLUTION

deepakgarg
ServiceNow Employee

Hi Rhonda,



If this is the case, then you can simply convert both the variables to GlideDate first and then compare.


Change given Script include with this:


var ClientDateTimeUtils = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getDateTimeDiff: function(){


  var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field


  var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field


  var d1 = new GlideDateTime(firstDT).getDate();


  var d2 = new GlideDateTime(secondDT).getDate();


  return gs.dateDiff(d1, d2, true);


  },


});



And change your client script as:


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


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


  return;


  }


  var sdt = g_form.getValue('d1'); //Second Date/Time field or start_date


  var fdt = g_form.getValue('devent'); //first Date field or event



  var ajax = new GlideAjax('ClientDateTimeUtils');


  ajax.addParam('sysparm_name','getDateTimeDiff');


  ajax.addParam('sysparm_fdt', fdt);


  ajax.addParam('sysparm_sdt', sdt);


  ajax.getXML(doSomething);



  function doSomething(response){


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


  if(parseInt(answer) != 0)


  alert('Event should start on same day!');


  }


}



And it's done. It will only match date part & truncate the time part.


View solution in original post

18 REPLIES 18

deepakgarg
ServiceNow Employee

Hi Rhonda,



If this is the case, then you can simply convert both the variables to GlideDate first and then compare.


Change given Script include with this:


var ClientDateTimeUtils = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getDateTimeDiff: function(){


  var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field


  var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field


  var d1 = new GlideDateTime(firstDT).getDate();


  var d2 = new GlideDateTime(secondDT).getDate();


  return gs.dateDiff(d1, d2, true);


  },


});



And change your client script as:


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


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


  return;


  }


  var sdt = g_form.getValue('d1'); //Second Date/Time field or start_date


  var fdt = g_form.getValue('devent'); //first Date field or event



  var ajax = new GlideAjax('ClientDateTimeUtils');


  ajax.addParam('sysparm_name','getDateTimeDiff');


  ajax.addParam('sysparm_fdt', fdt);


  ajax.addParam('sysparm_sdt', sdt);


  ajax.getXML(doSomething);



  function doSomething(response){


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


  if(parseInt(answer) != 0)


  alert('Event should start on same day!');


  }


}



And it's done. It will only match date part & truncate the time part.


Thank you very much; this appears to have done the trick.



Thanks,


Rhonda


deepakgarg
ServiceNow Employee

Glad, it worked.


Eli Guttman
Tera Guru

Hi,

 

I'm running into similar issues - seems like the user in ServicePortal can edit the time of  a datetime variable and submit the form without proper validation.

In my case, since the date/time control is sometimes complicated for the user, his prefer to select the date and type the hours

the problem is that he type it in this format: "2018-05-09 09:00" and not "2018-05-09 09:00:00".

After submit, the system doesn't know how to interpret the value, and ignore the time part, so eventually, in the system we get this value:


"2018-05-09 00:00:00"

 

Does anyone know how to handle such case?