The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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
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

Abhinay Erra
Giga Sage

So you only want to check the date part of the start Time right?


Geoffrey2
ServiceNow Employee
ServiceNow Employee

Why not replace the Date/Time variables with Select Boxes populated with times in 15 minute intervals (or something)? If you need to populate the times into Date/Time fields you can just append your 3 time inputs to the one Date input before populating it into whatever Date/Time fields you're using.


deepakgarg
ServiceNow Employee
ServiceNow Employee

Hi Rhonda,



You can create an onChange Catalog client script on 'start time' variable to achieve this.



First create a Script Include as:


Name:ClientDateTimeUtils


Client Callable: true


var ClientDateTimeUtils = Class.create();  


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


 


//Takes a Single Date/Time Field and returns its time difference from nowDateTime().  


//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)  


getNowDateTimeDiff: function(){  


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


var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day  


var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);  


var timediff = this._calcDateDiff(diffTYPE, diff);  


//return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;  


return timediff;  


},  


 


//Diff the amount of time between two different Date/Time fields  


//params = sysparm_fdt (the first date/time field), sysparm_sdt (second date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)  


getDateTimeDiff: function(){  


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


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


var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day  


var diff = gs.dateDiff(firstDT, secondDT, true);  


var timediff = this._calcDateDiff(diffTYPE, diff);  


//return "getDateTimeDiff: FIRST DT: " + firstDT + " -SECOND DT: " + secondDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;  


return timediff;  


},  


 


//Takes your date/time field and returns the amount of time before now. A positive is time before now, a negative number is after now.  


//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)  


getDateTimeBeforeNow: function(){  


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


var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day  


var diff = gs.dateDiff(firstDT, gs.nowDateTime(), true);  


var timediff = this._calcDateDiff(diffTYPE, diff);  


//return "getDateTimeBeforeNow: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;  


return timediff;  


},  


 


//Returns true if it is before now, and false if it is after now.  


//params = sysparm_fdt (the first date/time field)  


getDateTimeBeforeNowBool: function(){  


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


var diff = gs.dateDiff(firstDT, gs.nowDateTime(), true);  


var answer = '';  


if (diff >= 0){answer = 'true';}  


else {answer = 'false';}  


return answer;  


},  


 


//Returns the Date/Time of right now.  


getNowDateTime: function(){  


var now = gs.nowDateTime(); //Now Date/Time  


return now;  


},  


 


//Returns the Date right now.  


getNowDate: function(){  


var now = GlideDate();; //Now Date  


return now.getLocalDate();  


},  


 


//Returns the Time of right now.  


getNowTime: function(){  


var now = GlideTime();; //Now Time  


var modnow = now.getLocalTime().toString().split(' ');  


return modnow[1];  


},  


 


//Takes a date/time field and adds time to it.  


//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - second, minute, hour, day, week, month, year), sysparm_addtime (amount of time to add based on the type).  


addDateTimeAmount: function(){  


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


var addTYPE = this.getParameter('sysparm_addtype'); //What to add - second (addSeconds()), minute (need to add conversion), hour (need to add conversion), day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())  


var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  


var day = GlideDateTime(firstDT);  


 


if(addTYPE == 'second'){day.addSeconds(addTIME);}  


else if (addTYPE == 'minute'){day.addSeconds(addTIME*60);}  


else if (addTYPE == 'hour'){day.addSeconds(addTIME*(60*60));}  


else if (addTYPE == 'day'){day.addDays(addTIME);}  


else if (addTYPE == 'week'){day.addWeeks(addTIME);}  


else if (addTYPE == 'month'){day.addMonths(addTIME);}  


else if (addTYPE == 'year'){day.addYears(addTIME);}  


else {day.addDays(addTIME);}  


 


//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;  


return day;  


},  


 


//Takes a glide date field and adds time to it.  


//params = sysparm_fdt (the first date/time field), sysparm_addtype (type of time to add - day, week, month, year),sysparm_addtime (amount of time to add based on the type).  


addDateAmount: function(){  


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


var addTYPE = this.getParameter('sysparm_addtype'); //What to add - day (addDays()), week (addWeeks()), month (addMonths()), year (addYears())  


var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  


var day = GlideDate();  


day.setValue(firstDT);  


 


if(addTYPE == 'day'){day.addDays(addTIME);}  


else if (addTYPE == 'week'){day.addWeeks(addTIME);}  


else if (addTYPE == 'month'){day.addMonths(addTIME);}  


else if (addTYPE == 'year'){day.addYears(addTIME);}  


else {day.addDays(addTIME);}  


 


//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + day;  


return day;  


},  


 


addTimeAmount: function(){  


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


var addTYPE = this.getParameter('sysparm_addtype'); //What  


var addTIME = this.getParameter('sysparm_addtime'); //How much time to add  


var time = GlideTime();  


time.setValue(firstDT);  


 


if(addTYPE == 'second'){time.addSeconds(addTIME);}  


else if (addTYPE == 'minute'){time.addSeconds(addTIME*60);}  


else if (addTYPE == 'hour'){time.addSeconds(addTIME*(60*60));}  


else {time.addSeconds(addTIME);}  


 


var modtime = time.toString().split(' ');  


//return "First Date: " + firstDT + " -Time to Add: " + addTIME + " -Add Type: " + addTYPE + " -Added Time: " + time;  


return modtime[1];  


},  


 


//Private function to calculate the date difference return result in second, minute, hour, day.  


_calcDateDiff: function(diffTYPE, seconds){  


var thisdiff;  


if (diffTYPE == "day"){thisdiff = seconds/86400;}  


else if (diffTYPE == "hour"){thisdiff = seconds/3600;}  


else if (diffTYPE == "minute"){thisdiff = seconds/60;}  


else if (diffTYPE == "second"){thisdiff = seconds;}  


else {thisdiff = seconds;}  


return thisdiff;  


}


});


Though you don't need all functions for now. But they might be helpful in future.


Screen Shot 2016-09-20 at 12.03.14 PM.png


Now, create an onchange catalog client script on the start date variable 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 dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.



  var ajax = new GlideAjax('ClientDateTimeUtils');


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


  ajax.addParam('sysparm_fdt', fdt);


  ajax.addParam('sysparm_sdt', sdt);


  ajax.addParam('sysparm_difftype', dttype);


  ajax.getXML(doSomething);



  function doSomething(response){


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


  if(parseInt(answer) != 0)


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


  }


}


Screen Shot 2016-09-20 at 12.05.14 PM.png



Make sure to use correct variable names.



For reference: Client Script Date/Time Functions


I simply edited the script for your use case.



Thanks,


Deepak.


PS: Hit like, Helpful or Correct depending on the impact of the response.


Pradeep J
Kilo Guru

Hi Rhonda,



Here I have tried as per your requirement. And Its working as expected .



Total 4 Variable on Catalog.


1.Event Date of type Date


2.StartTime of Type Date/Time


3.SetTime of Type Date/Time


4.EndTime of Type Date/Time



catalog.PNG



I have written Catalog client script on Start Time variable onChange()Script.PNG



Code :



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


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


          return;


    }




    //fetch the eventdate value in a variable


  var eventdate=g_form.getValue("eventdate");


  alert(eventdate);



  //fetch the starttime value in a variable


  var start = g_form.getValue("starttime");


  alert(start);



  //extract the date


  var res = start.substring(0,10);


  alert(res);



  //Convert it into Date


  var eDate=new Date(eventdate);


  var rDate=new Date(res);



  alert("Dates are"+ eDate+""+ rDate);



  //Compare two dates, if Date is same alert the box saying   that DAte is same and allow to write the vale


  if(Date.parse(eDate)== Date.parse(rDate))


  {


  alert("date is same");


  }



  //Cleat the value and allow to select the date


  else


  {


  alert("date is diff");


  g_form.setValue("starttime","");


  }


}




PS: Hit like, Helpful or Correct depending on the impact of the response



Thanks


Pradeep D J