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:
= GlideDateTime.subtract(.getDisplayValue(),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. For any date or date/time field in the SNC database, use getDisplayValue() in GlideRecord to properly format the data.

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('','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('');var end = g_form.getValue('');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('', 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;
current.duration.setDateNumericValue(timems);
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:
format=glide_duration
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:
max_unit=