Compare a field date with current time using Client Script.

prabhmeet
Giga Expert

Hi,

I want that on change_request table, planned start date should always be 24hrs ahead of current time.

I have done this successfully using Business Rule, But I have to do this using Client Script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below


var start = g_form.getValue('start_date');
var dttype = 'hour';
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', start);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);

function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer >= 1)

{g_form.addErrorMessage('Selected time should be more than 24 hours of current time');

}
}

it is not working. Can someone help with the code?

 

1 ACCEPTED SOLUTION

I see, if you only want to write in client script. You can using pure javascript in stead of ajax call.

Below is the sample: You write this in your client script then you should able to get day ago date and from there u can continue your checking logic.

var d = new Date();
d.setDate(d.getDate() - 1);
alert(d.toString());

View solution in original post

16 REPLIES 16

Ravi T
Tera Guru

Hi

Can you share the business rule code here?

 

Regards

Ravindra

This is the code

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here

if(current. start_date<= gs.daysAgo(-1)) {


current.setAbortAction(true);


gs.addInfoMessage("The planned start date must be more than 1 calendar days in the future.");
}


})(current, previous);

Jeff Currier
ServiceNow Employee
ServiceNow Employee

This post is nearly what you are asking for, you just need to add 24 hours and you have what you need.

 

This article on date/time functions is also a good reference for you.

Can you please write and show me the changes, because I do not know how to do that