Set a duration field value

Some examples of JavaScript that can be used to set the value of a duration field.

Note: Negative duration values are not supported.

The dateDiff() method

The dateDiff(start, end, false) method in GlideSystem enables you to set the duration value using a given start date/time and end date/time. An example to set the duration for the time a task was opened would be:
current.calendar_duration= gs.dateDiff(current.opened_at.getDisplayValue(),current.closed_at.getDisplayValue(),false);
The boolean value at the end of the call indicates the type of duration value to return:
  • false = return the duration value
  • true = return the difference between the two times in seconds; returned as a character or string value.
If you want to work with the value returned as a number to be used in date or duration aritmetic then use code similar to the following to convert the string value returned to a millisecond value:
var time = gs.dateDiff(<start_field>.getDisplayValue(),gs.nowDateTime(),true);
time = parseFloat(time)*1000;
If you wanted to set a duration to the amount of time between some event and the current date/time, use code similar to the following:
<duration_field>= gs.dateDiff(<start_field>.getDisplayValue(),gs.nowDateTime(),false);

The time values presented to dateDiff() are expected to be in the user's timezone and in the user's format. For any date or date/time field in the SNC database, use getDisplayValue() in GlideRecord to properly format the data.

If you want to do your own calculations and set the value of a duration field the best way would be to use setDisplayValue() to set the value for the field. For a duration field, setDisplayValue() expects a character string value with a format of ddd hh:mm:ss where ddd is the number of days for the duration. For example, to set the duration field called u_time_to_assign in the current record to a value of 3 days 4 hours 30 minutes and 14 seconds you would:
current.u_time_to_assign.setDisplayValue('3 04:30:14');

Setting a default value of a duration field

Setting the default value for a duration field is similar to the method used above.

Setting the value of a duration field in a client script

This script sets a duration_field value in a client script. Replace duration_field with the field name from your instance.
g_form.setValue('<duration_field>','11 01:02:03');

Calculating a duration and setting that value using a client script

Here's an example of how to use dateDiff() to return a value and populate it using a client script.

Create an onChange client script that includes the following code. You can modify this script if you need the calculation to happen in an onLoad script or some other way.
function onChange(control, oldValue, newValue, isLoading){var strt = g_form.getValue('<start_field>');var end = g_form.getValue('<end_field>');var ajax =new GlideAjax('AjaxDurCalc');
  ajax.getXMLWait();var answer = ajax.getAnswer();
  g_form.setValue('<duration_field>', answer);}
Create a system script include file called AjaxDurCalc that handles the request. It may be reused for other functions as well.
var AjaxDurCalc =Class.create();
AjaxDurCalc.prototype= Object.extendsObject(AbstractAjaxProcessor,{
 durCalc:function(){return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'),false);}});

Changing the Duration Field Value

If you manipulate a duration value with addition/subtraction of some amount of time, then you can use the functions that allow you to get and set the numeric value of the duration. A unit of measure for a duration numeric value is milliseconds. Below is an example that adds 11 seconds to the duration field in the current record.
var timems = current.duration.dateNumericValue();
timems = timems + 11*1000; 

Format the Resolve Time

To format the Resolve Time or the Business Resolve Time fields as durations, which displays them as a duration instead of a large integer, add the following attribute to those fields:

Modify the dictionary entry for the field and add the attribute. If there is an existing attribute, separate multiple attributes with commas.

Set the Maximum Unit of Measurement

The max_unit dictionary attribute defines the maximum unit of time used in a duration. For example, if max_unit=minutes, a duration of 3 hours 5 minutes 15 seconds displays as 185 minutes 15 seconds. To set the maximum unit of duration measurement add the following dictionary attribute to the duration field: