modify date to numeric value

Not applicable

I select the current date into a field for example

var vAcquired = g_form.getValue('u_acquired');


// Retrieving Date from System
var vrightnow = AJAXEvaluateSynchronously('gs.now()');
g_form.setValue('u_calcequipdate', vrightnow);


vAcquired is the field I want to make numeric so I can take

var newDate = vrightnow - vAcquired. - this should give me the total days between the 2 fields.

2 REPLIES 2

john_andersen
Tera Guru

I would make use of the newer "GlideAjax" methods found at:
http://wiki.service-now.com/index.php?title=GlideAjax

I would also employ some built in GlideSytem Date libraries:
http://wiki.service-now.com/index.php?title=GlideSystem_Date_and_Time_Functions

If I were to take the date value of a field on my form and have another field show the different between that date and now (in days), I would do the following.

I would build out a "Script Include" that was named "MyDateCalculator". I would set it to "Client Callable". The script would look something like:



var MyDateCalculator = Class.create();

MyDateCalculator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

dateDiffNow : function() {
var startDate = this.getParameter("sysparm_start");
var now = gs.nowDateTime();
var diff = gs.dateDiff(startDate, now, true);
return diff;
}

});


I would then create a Client Script on my form that is watching for an onChange event on the field where I will be entering a date. In my example, the field would be "Start".



function onChange(control, oldValue, newValue, isLoading) {
//If the page isn't loading
if (!isLoading) {
//If the new value isn't blank
if(newValue != '') {
var start = g_form.getValue('u_start');
var ga = new GlideAjax('MyDateCalculator');
ga.addParam('sysparm_name', "dateDiffNow");
ga.addParam('sysparm_start', start);
ga.getXML(FunctionParse);
}
}
}

function FunctionParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var days = answer/60/60/24;
g_form.setValue("u_smaller_number", days);
}


In this example, when the "Start" field has a date added to it, I use GlideAjax to take that date, compare to now and return the number of seconds between the two dates.

The answer is then pushed through the "FunctionParse" function where the answer is divided a number of times to get us a unit of days. It then sets the number of days in a field on my form called "u_smaller_number".

I hope this helps!

-John


Thanks this worked.