Show a field if Date/Time field is 'more than 12 hours' from nowDateTime?

Josh80
Tera Expert

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!

21 REPLIES 21

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


Hi...






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


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_why_is_issue_urgent', false);


//g_form.setVisible('u_more_escalation_info', false);


alert('Good to GO');


  }  


}


}




var MyDateTimeAjax = Class.create();


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;


}


});


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


find_real_file.png


find_real_file.png



Given less than 8 hours in issue start time:



find_real_file.png


find_real_file.png


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 <


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?