Calculate duration of two Date/Time fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2017 05:42 PM
Hi all,
On the Incident table, I have two Date/Time fields called Outage Start and Outage End. I also have a time field called Total Outage Time.
My requirements are that the Total Outage Time to be automatically calculated from the Outage Start and Outage End fields. If the start or end times change, I would like the total outage time to reflect these changes.
Fields:
Title | Name | Type |
---|---|---|
Outage Start | u_outage_start | Date/Time |
Outage End | u_outage_end | Date/Time |
Total Outage Time | u_total_outage_time | Time |
What is the best way to tackle this? I have seen a few threads and tried a few examples were for onBefore business rules, that did not seem to work, however, this could be me not the code.
Calculating a duration from 2 date / time fields in a scoped application
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
current.u_total_outage_time = GlideDateTime.subtract(new GlideDateTime(current.getValue("u_outage_start")), new GlideDateTime(current.getValue("u_outage_end")));
current.update();
}
Need to calculate difference of date/time fields
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
var outage_start = current.u_outage_start.getDisplayValue();
var outage_end = current.u_outage_end.getDisplayValue();
//If the outage start and end times are present, calculate the duration
if (reported) {
var difference = gs.dateDiff(outage_start, outage_end, false);
current.u_total_outage_time.setValue(difference);
}
}
Thanks in advance,
Brendan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2017 05:50 PM
Hi Brendan,
Did you try using OnChange Client script. Refer to this excellent article Client Script Date/Time Functions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2017 08:41 PM
Hi Bharath,
cheers for that.
I have put together an on submit client script and script include, and it works perfectly, with one caveat. If one field is empty, it throws an error:
OnSubmit Client Script
function onSubmit() {
var outage_start = g_form.getValue('u_outage_start'); //set this as the outage start date
var outage_end = g_form.getValue('u_outage_end'); //set this as the outage end date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',outage_start);
ajax.addParam('sysparm_end',outage_end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_total_outage_time', answer); //set this as the total outage time
}
Script Include:
Name: AjaxDurCalc
Client callable: Yes
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});
How can i tell the script to ignore null entries, and to only try to calculate when both fields are populated?
Cheers,
Brendan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2017 10:21 PM
ok, so I have changed my script to be an OnChange client script, basing it off the Outage End field as Puja has suggested.
However, when I go into the incident, the error still appears.
OnChange Client Script
Field Name: Outage End
function onChange(control, oldValue, newValue, isLoading) {
var outage_start = g_form.getValue('u_outage_start'); //set this as the outage start date
var outage_end = g_form.getValue('u_outage_end'); //set this as the outage end date
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',outage_start);
ajax.addParam('sysparm_end',outage_end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_total_outage_time', answer); //set this as the total outage time
}
Script Include:
Name: AjaxDurCalc
Client callable: Yes
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});
Does anyone know how I can stop this error from occurring?
Cheers,
Brendan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2017 11:05 PM
function onChange(control, oldValue, newValue, isLoading) {
var strt = g_form.getValue('u_outage_start');
var end = g_form.getValue('u_outage_end');
var ajax = new GlideAjax('AjaxDurCalc');
ajax.addParam('sysparm_name','durCalc');
ajax.addParam('sysparm_strt',strt);
ajax.addParam('sysparm_end',end);
ajax.getXMLWait();
var answer = ajax.getAnswer();
g_form.setValue('u_total_outage_time', answer);
}
Script include
Name:AjaxDurCalc
Client callable:checked
script:
var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
durCalc: function() {
return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);
}
});
This should work, I am not sure why you are getting null error.