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

Shishir Srivast
Mega Sage

Hi Josh,



Please try below code, an onChange client script on the field where you are changing the date and validating with current datetime



onChange Client Script


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


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


return;


}


//Type appropriate comment here, and begin script below


var opdt = g_form.getValue('opened_at'); //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'){


alert('Selected time is more than 12 hours');


}


else


alert('Good to GO');


}


}



Script Include:


var MyDateTimeAjax = Class.create();


MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


nowDateTime: function () {


var seconds = '43200'; //no Seconds in 12 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;


}


});


Hello Shishir!



Thank you for your response; If you could provide a bit more info...



I have field 'Issue Start Time' = u_issue_start_time



If the 'Issue Start Time' exceeds 8 hours ago from current time, I want to display a field.



Can you help with that?




Thanks again!


In that case you can changed to onLoad() client script with you custom field, update the code hope this helps.



onLoad Client Script


function onLoad() {


//Type appropriate comment here, and begin script below


var istdt= g_form.getValue('u_issue_start_time');


var ajax = new GlideAjax('MyDateTimeAjax');


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


ajax.addParam('sysparm_dt', istdt);


ajax.getXML(ajaxResponse);


function ajaxResponse(serverResponse) {


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


if(answer == 'true'){


alert('Issue start time has exceeded 8 hours');


}


else


alert('Good to GO');


}


}




Script Include:


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;


}


});


thanks! I almost have it...fields don't appear though if hours exceed 8


hours...



They should appear if start time is greater than 8 hours ago.



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