Thank you for your feedback.
Form temporarily unavailable. Please try again or contact to submit your comments.
  • London
  • Kingston
  • Jakarta
  • Istanbul
  • Helsinki
  • Geneva
  • Store

Set a duration field value

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.

Using the GlideDateTime.subtract() method

The subtract(GlideDateTime start, GlideDateTime end) method in GlideDateTime enables you to set the duration value using a given start date/time and end date/time. An example on how to set the duration for the time a task was opened is:
var duration = GlideDateTime.subtract(start, end);
If you want to work with the value returned as a number to use in date or duration arithmetic, convert the return to milliseconds:
var time = GlideDateTime.subtract(start,end).getNumericValue();
If you want to set a duration to the amount of time between some event and the current date/time:
<duration_field> = GlideDateTime.subtract(new GlideDateTime(<start_time>.getValue()),gs.nowDateTime());

The time values presented to GlideDateTime.subtract are expected to be in the user's time zone and in the user's format.

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: