Scoped GlideDuration API Reference

From ServiceNow Wiki
Home > Build > Applications > Scoped System API > Scoped GlideDuration API Reference
Jump to: navigation, search
Scoped Application Scripting
Related Topics
Get the Book
Knowledge.gif Scripting in Scoped Applications

1 Overview

The scoped GlideDuration class provides methods for working with spans of time or durations. GlideDuration objects store the duration as a date and time from January 1, 1970, 00:00:00. As a result, setValue() and getValue() use the GlideDateTime object for parameters and return values.

The scoped GlideDuration API is available starting with the Fuji release.

NEW: You can also find this class documented in the easy-to-read API reference on the Developer Program web site: Scoped GlideDuration.

2 Method Summary

Return Type Method
GlideDuration add(GlideDuration value)
Adds a given duration to the current duration.
String getByFormat(String format)
Gets the duration in the given format.
Number getDayPart()
Gets the day value of the duration.
Number getRoundedDayPart()
Gets the rounded number of days.
String getDisplayValue()
Gets the display value of the duration in number of days, hours, and minutes.
String getDurationValue()
Gets the duration value in d days HH:mm:ss format.
String getValue()
Gets the date and time value in the internal format.
void setDisplayValue(String asDisplayed)
Sets the display value.
void setValue(Object o)
Sets the internal value.
GlideDuration subtract(GlideDuration value)
Subtracts a given duration from the current duration.

3 Constructors

3.1 GlideDuration()

Instantiates a GlideDuration object.

Parameters:

  • None

3.2 GlideDuration(GlideDuration another)

Instantiates a GlideDuration object by cloning the value of another GlideDuration object.

Parameters:

  • another - (GlideDuration) another GlideDuration object.

3.3 GlideDuration(Number ms)

Instantiates a GlideDuration object with the given duration.

Parameters:

  • ms - (Number) the duration value, in milliseconds.

3.4 GlideDuration(String displayValue)

Instantiates a GlideDuration object with the given display value

Parameters:

  • displayValue - (GlideDuration) the display value for the constructed object

4 Method Details

4.1 add(GlideDuration value)

Adds a given duration to the current duration.

Parameters:

  • value - (GlideDuration) the value to add.

Returns:

GlideDuration - the sum of the current duration and the added duration.

Example:

var duration = new GlideDuration('3 12:00:00');
var duration2 = new GlideDuration('3:00:00');
var answer = duration.add(duration2);
gs.info(answer.getDisplayValue());

Output:

3 Days 15 Hours

4.2 getByFormat(String format)

Gets the current duration in the given format.

Parameters:

  • format - (String) the duration format.

Returns:

String - the current duration in the specified date format.

Example:

var dur = new GlideDuration('3 22:00:00');
gs.info(dur.getByFormat('HH:mm'));

Output:

22:00

4.3 getDayPart()

Gets the number of days

Parameters:

  • None

Returns:

Number - the number of days

Example:

var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDayPart());

Output:

3

4.4 getRoundedDayPart()

Gets the rounded number of days. If the time part is more than 12 hours, the return value is rounded up. Otherwise, it is rounded down.

Parameters:

  • None

Returns:

Number - the day part, rounded.

Example:

var dur = new GlideDuration('3 11:00:00');
gs.info(dur.getRoundedDayPart());

Output:

3

4.5 getDisplayValue()

Gets the display value of the duration in number of days, hours, and minutes.

Parameters:

  • None

Returns:

String - the number of days, hours, and minutes.

Example:

var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDisplayValue());

Output:

3 Days 12 Hours

4.6 getDurationValue()

Gets the duration value in "d HH:mm:ss" format.

Parameters:

  • None

Returns:

String - the duration value.

Example:

var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDurationValue());

Output:

3 12:00:00

4.7 setDisplayValue(String asDisplayed)

Sets the display value.

Parameters:

  • asDisplayed - (String) the duration value in "d HH:mm:ss" format

Returns:

void

Example:

var dur = new GlideDuration(); 
dur.setDisplayValue('3 08:00:00');
gs.info(dur.getDisplayValue());

Output:

3 Days 8 Hours

4.8 getValue()

Gets internal value of the this duration object. GlidDuration is stored as DateTime, see "Overview" section above.

Parameters:

  • None

Returns:

String - the duration value in the internal format.

Example:

var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getValue());

Output:

1970-01-04 12:00:00

4.9 setValue(Object o)

Sets the internal value of the GlideDuration object. Internally, GlidDuration is stored as DateTime, see "Overview" section above.

Parameters:

  • o - (Object) the value for the GlideDuration object.

Returns:

void

Example:

var dur = new GlideDuration();
dur.setValue('1970-01-05 08:00:00'); // sets internal DateTime value. The String will be parsed into a GlideDateTime object.
gs.info(dur.getDisplayValue());

Output:

4 Days 8 Hours

4.10 subtract(GlideDuration value)

Subtracts a given duration from the current duration.

Parameters:

  • value - (Scoped GlideDuration) the duration to subtract.

Returns:

void

Example:

var duration = new GlideDuration('3 12:00:00');
var duration2 = new GlideDuration('3:00:00');
var answer = duration.subtract(duration2);
gs.info(answer.getDisplayValue());

Output:

3 Days 9 Hours
Was this article helpful?
Yes, I found what I needed
No, I need more assistance