- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2016 07:06 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2016 01:28 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2016 01:28 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2016 01:29 PM
Thank you very much; this appears to have done the trick.
Thanks,
Rhonda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2016 01:41 PM
Glad, it worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2018 03:29 AM
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?