Show a field if Date/Time field is 'more than 12 hours' from nowDateTime?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2017 07:34 PM
Hi - I am not that great at date/time scripts.
I am looking for a client script to compare a custom date/time field and if the date/time is more than 8 hours ago, I want to display a field.
Can someone help me out/provide guidance?
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 02:40 PM
Hm - I see the gs.log(diff) in script, but when I check ALL logs including Scripting logs, there are no messages containing 'diff'.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2017 02:36 PM
Hi...
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var opdt = g_form.getValue('u_issue_start_time'); //Replace the date field name accordingly
var ajax = new GlideAjax('MyDateTimeAjax');
ajax.addParam('sysparm_name', 'nowDateTime');
ajax.addParam('sysparm_dt', opdt);
ajax.getXML(ajaxResponse);
function ajaxResponse(serverResponse) {
var answer = serverResponse.responseXML.documentElement.getAttribute("answer");
if(answer == 'true'){
//g_form.setVisible('u_why_is_issue_urgent', true);
//g_form.setVisible('u_more_escalation_info', true);
alert('Selected time is more than 12 hours');
}
else {

//g_form.setVisible('u_more_escalation_info', false);
alert('Good to GO');
}
}
}
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nowDateTime: function () {
var opdt = this.getParameter('sysparm_dt');
var seconds = '28800'; //no Seconds in 8 hours
var gdt = new GlideDateTime(opdt);
var nowdt = new GlideDateTime();
var diff = gs.dateDiff(nowdt, gdt, true);
gs.log(diff);
if(parseInt(diff) < parseInt(seconds))
return true;
else
return false;
}
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2017 06:24 PM
Hi Josh,
I tried to replicate the issue on catalog with same Catalog Client Script and Script Include but it did just worked for me, but also I have noticed that it is calculating based upon the GMT time which is 7 hours ahead from me (PDT). So, I just reduced the number of hrs to compare to 1 hr (3600 seconds) [7hrs(GMT and PDT difference) + 1 = 8 hrs(validation time)] in script include. I would suggest to calculate the seconds based upon the difference of GMT and your TimeZone.
var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nowDateTime: function () {
var opdt = this.getParameter('sysparm_dt');
var seconds = '3600'; //no Seconds for 1 hour [7hrs(GMT and PDT difference) + 1 = 8 hrs(validation time)]
var gdt = new GlideDateTime(opdt);
var nowdt = new GlideDateTime();
gs.log('nowdt' + nowdt);
var diff = gs.dateDiff(nowdt, gdt, true);
gs.log(diff);
if(parseInt(diff) < parseInt(seconds))
return true;
else
return false;
}
});
Result and Logs:
Given more than 8 hours in issue start time
Given less than 8 hours in issue start time:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2017 06:59 PM
Hi!
Can you clarify where the client script is calling the script include?
I thought it was the 'glideajax' name MyDateTimeAjax in client script, so I
named the script include 'MyDateTimeAjax'
I do not see how the client script is actually calling the script include
in a function?
thanks
On Sun, Jul 16, 2017 at 9:25 PM, explorenow <
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2017 07:05 PM
It's ok - I validated I was calling it correctly.
Still not working thought; I can't do anything to invoke an answer of
'true', that the hours exceed 8 in the past.
Shouldn't 'hoursAgo' be in the script somewhere?