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

Hi Shishir



I did update it.   No dice; fields do not display...they remain hidden even when the field is changed.


I left both client scripts in place (onChange and onLoad).



var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


nowDateTime: function () {


var seconds = '28800'; //no Seconds in 8 hours


var opdt = this.getParameter('sysparm_dt');


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


if(parseInt(diff) > parseInt(seconds))


return true;


else


return false;


}


});


On question: I did enter a date within 12 hours and the fields disappeared, but when I changed the date to outside 12 hours, the fields did not appear.



I think that's the expected behavior of the code, if issue start time is less the 12 hours then return from script include would be false and then the fields (u_why_is_issue_urgent and u_more_escalation_info) will be set to visible false (means disappear) and when start time is greater the 12 hours then return from script include would be true and then the fields (u_why_is_issue_urgent and u_more_escalation_info) will be set to visible true (means do not disappear)



if you want the field to disappear when it exceeds 8 hours and should not be hidden when it's with-in 8 hours then change the if condition in script include with below code and try.



if(parseInt(diff) < parseInt(seconds)) // diff should be less than 8 hours so that it will returns true to make the field visible else return false to hide the field


Hm I commented out my setting fields visible etc, and left in your


'Alert' lines of code.



It doesn't matter if the date is within 8 hours or exceeds 8 hours, I get


the 'Good to Go' alert prompt, regardless.



On Sat, Jul 15, 2017 at 3:29 PM, explorenow <


If I add 'else if' answer is false, then 'good to go' then no prompts are


displayed.



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 if(answer == 'false') {


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


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


alert('Good to GO');


}


}


}


hmm.



when i select the date less then 12 hours then it give me pop-up below.


find_real_file.png


if i put tomorrow date (more then 12 hours) then i get Good to Go pop-up


find_real_file.png



I am using below, client script


find_real_file.png


script include:


find_real_file.png