You are on page 1of 15

Vancouver API Reference

DurationCalculator - Global
The DurationCalculator API provides methods for calculating durations
and due dates.

DurationCalculator - calcDuration(Number seconds)

Calculates an end date and time based on current start date and time
and passed in duration (seconds).

This method also sets the this.endDateTime, this.seconds, and


this.totalSeconds properties with the updated values. Prior to calling this
method, you must call setStartDateTime() with the start time to use in the
computation.

Parameters

Name Type Description

Number of seconds to
add to the start date
and time to compute
seconds Number
the end date and
time, seconds, and
total seconds values.

Returns

Type Description

False if the input value is not a


Boolean
number.

Example

var gdt = new GlideDateTime("2020-05-01 00:00:00");

var dc = new DurationCalculator();


dc.setStartDateTime(gdt);

if(!dc.calcDuration(2*24*3600)){ // 2 days

PDF generated on August 20, 2023 3


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

gs.log("*** Error calculating duration");


return;
}
var secs = dc.getSeconds();
var totalSecs = dc.getTotalSeconds();

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSEC


S=" + totalSecs + " ENDTIME = " + endDateTime);

Output

***SCHEDULE DURATION: SECS=172800 TOTALSECS=970534 ENDTIM


E = 2020-05-03 00:00:00

DurationCalculator - calcRelativeDueDate(GlideDateTime start,


Number days, String endTime)

Calculates the due date starting at the passed in start time and adding
the number of days using the current schedule and time zone.

Called from relative duration definitions,


initiated by calcRelativeDuration(), as
calculator.calcRelativeDueDate(calculator.startDateTime,
days). Once the day that the work is due is determined, the method sets
the time to the passed in endTime of that day. If there are not enough
days left in the schedule, uses the last day in the schedule.

Parameters

Name Type Description

GlideDateTime object
that contains the
start GlideDateTime
start date for the
computation.

Number of days to
days Number
add to the start date.

Time of day that the


endTime String work is due on the
computed due date.

PDF generated on August 20, 2023 4


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Name Type Description


If blank, defaults to the
end of the work day.

Format: HH:mm:ss

Returns

Type Description

Flag that indicates if the


completion date is within the
schedule.
Possible values:

• true: Completion date is within


Boolean the schedule.

• false: Completion date falls


outside the schedule. Undefined
if no schedule was set prior to
calling this method.

Example

var dc = new DurationCalculator();


var startTime = new GlideDateTime();

// Settings for calculations


// Optional: Specify the schedule to use for the followin
g calculations
dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
// Optional: Specify a different timezone to use
dc.setTimeZone("Los Angeles");
// Optional: Set a start date and time, otherwise the curr
ent time is assumed
dc.setStartDateTime("2020-04-10 08:00:00")
// Calculate end time, from number of seconds required in
the schedule

PDF generated on August 20, 2023 5


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

dc.calcDuration(3*24*3600); // 3 days
dc.calcRelativeDueDate(startTime, "3", "07:00:00");

DurationCalculator -
calcRelativeDuration(String relativeDurationID)

Calculates the duration using the specified relative duration script.

Upon completion, the this.endDateTime and this.seconds properties are


set to indicate the results of the calculation.

Parameters

Name Type Description

sys_id of relative
duration schedule
relativeDurationID String (table
cmn_relative_duration
).

Returns

Type Description

Boolean The result of the duration script.

Example

var dc = new DurationCalculator();


dc.calcRelativeDuration('08fcd0830a0a1b2600074f56b1ad7cb')
;

DurationCalculator - calcScheduleDuration(String startTime,


String endTime)

Returns the duration between the startTime and the endTime within the
already-specified schedule and optionally overridden timezone.

This method also sets this.endDateTime, this.seconds, and


this.totalSeconds in the current schedule object.

PDF generated on August 20, 2023 6


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Parameters

Name Type Description

Optional. Display
value for the end time.

Default: Uses the


current date and time
(set using
setStartDateTime()).
startTime String You must pass a
placeholder if this
parameter it not
passed, such as
dur.calcScheduleDu
ration("",
endDateTime);.

Optional. Display
value for the start time.

Default: Uses the


endTime String
current date and
time (set using
setEndDateTime()).

Returns

Type Description

Schedule duration. Returns 0 if


endTime is before startTime.
Number
Unit: Seconds

Example

var dur = new DurationCalculator();

PDF generated on August 20, 2023 7


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

// Set 9-5 weekday schedule. This is the schedule in whic


h endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration("2019-01-02 11:00:00", "2019-01-0
6 09:00:00");
var secs = dur.getSeconds();
var totalSecs = dur.getTotalSeconds();
var endDateTime = dur.getEndDateTime() + "";

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSEC


S=" + totalSecs + " ENDTIME = " + endDateTime);

Output

***SCHEDULE DURATION: SECS=97200 TOTALSECS=338400 ENDTIME


= 2019-01-06 09:00:00

DurationCalculator - calcScheduleDuration(GlideDateTime
startTime, GlideDateTime endTime)

Returns the duration between the startTime and the endTime within the
already-specified schedule and optionally overridden timezone.

This method also sets this.endDateTime, this.seconds, and


this.totalSeconds in the current schedule object.

Parameters

Name Type Description

Optional.
GlideDateTime object
that contains the start
time to use.

Default: Uses the


startTime GlideDateTime current date and time
(set using
setStartDateTime()).
You must pass a
placeholder if this
parameter it not
passed, such as

PDF generated on August 20, 2023 8


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Name Type Description


dur.calcScheduleDu
ration("",
endDateTime);.

Optional.
GlideDateTime object
that contains the end
time to use.
endTime GlideDateTime
Default: Uses the
current date and
time (set using
setEndDateTime()).

Returns

Type Description

Schedule duration. Returns 0 if


endTime is before startTime.
Number
Unit: Seconds

Example

var startDateTime = new GlideDateTime("2020-11-02 11:00:00


");
// Instantiate a new GlideDateTime object which has the en
d date as the current date and time
var endDateTime = new GlideDateTime();
var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in whic


h endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration(startDateTime, endDateTime);
var secs = dur.getSeconds();
var totalSecs = dur.getTotalSeconds();

PDF generated on August 20, 2023 9


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSEC


S=" + totalSecs + " ENDTIME = " + endDateTime);

Output

***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDT


IME = 2020-11-13 16:35:34

DurationCalculator - DurationCalculator( )

Constructor for DurationCalculator class.

Parameters

Name Type Description

None

Example

var dc = new DurationCalculator();

DurationCalculator - getEndDateTime( )

Gets the endDateTime property that was set by calcDuration/


calcRelativeDuration, indicating the end date and time for the duration.

Parameters

Name Type Description

None

Returns

Type Description

GlideDateTime The end datetime.

PDF generated on August 20, 2023 10


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Example

var dc = new DurationCalculator();


dc.calcDuration(52);
gs.print(dc.getEndDateTime());

2012-04-17 20:57:27

DurationCalculator - getSeconds( )

Returns the this.seconds property that was set by calcDuration/


calcRelativeDuration, indicating the total number of seconds of work to
be performed for the duration.

This is the total work time, not the total time between start and end times
and may be used to determine percentages of the work time.

Parameters

Name Type Description

None

Returns

Type Description

Number The total work time, in seconds.

Example

var startDateTime = new GlideDateTime("2020-11-02 11:00:00


");
// Instantiate a new GlideDateTime object which has the en
d date as the current date and time
var endDateTime = new GlideDateTime();
var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in whic


h endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration(startDateTime, endDateTime);
var secs = dur.getSeconds();

PDF generated on August 20, 2023 11


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

var totalSecs = dur.getTotalSeconds();

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSEC


S=" + totalSecs + " ENDTIME = " + endDateTime);

Output

***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDT


IME = 2020-11-13 16:35:34

DurationCalculator - getTotalSeconds( )

Returns the totalSeconds value that was set by a call to


calculate(record).

Parameters

Name Type Description

None

Returns

Type Description

Number The total number of seconds.

Example

var startDateTime = new GlideDateTime("2020-11-02 11:00:00


");
// Instantiate a new GlideDateTime object which has the en
d date as the current date and time
var endDateTime = new GlideDateTime();
var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in whic


h endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration(startDateTime, endDateTime);
var secs = dur.getSeconds();
var totalSecs = dur.getTotalSeconds();

PDF generated on August 20, 2023 12


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSEC


S=" + totalSecs + " ENDTIME = " + endDateTime);

Output

***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDT


IME = 2020-11-13 16:35:34

DurationCalculator - isAfter(GlideDateTime dt, String tm)

Compares the passed in time to the time value in the passed in


GlideDateTime object.

Parameters

Name Type Description

Either a GlideDateTime
object or a display
value date and time
to compare to the
GlideDateTime or passed in tm value.
dt
String If you pass a display
value date and time,
the method converts
it to a GlideDateTime
object.

Display value for the


time to compare
against the time value
in the GlideDateTime
tm String object. Assumes same
date.

Format: HH:mm:ss

PDF generated on August 20, 2023 13


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Returns

Type Description

Flag that indicates if the passed in


time value (tm) is after the time in
the GlideDateTime object (dt).
Possible values:
Boolean
• true: tm is after dt.

• false tm is not after dt.

Example

var dc = new DurationCalculator();


gs.print(dc.isAfter("2020-04-10 08:00:00", "09:00:00"));

*** Script: true

DurationCalculator - setSchedule(String schedId, String timezone)

Sets the schedule and time zone to use for calculating the due date.

Parameters

Name Type Description

Sys_id of the schedule


to set. Located
schedId String
in the Schedule
[cmn_schedule] table.

Optional. Time zone to


set.
timezone String
Default: User's time
zone.

PDF generated on August 20, 2023 14


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Returns

Type Description

void

Example

var startDateTime = new GlideDateTime("2020-11-02 11:00:00


");
// Instantiate a new GlideDateTime object which has the en
d date as the current date and time
var endDateTime = new GlideDateTime();
var dur = new DurationCalculator();

// Set 9-5 weekday schedule. This is the schedule in whic


h endDateTime, seconds, and totalseconds is set
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.calcScheduleDuration(startDateTime, endDateTime);
var secs = dur.getSeconds();
var totalSecs = dur.getTotalSeconds();

gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSEC


S=" + totalSecs + " ENDTIME = " + endDateTime);

Output

***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDT


IME = 2020-11-13 16:35:34

DurationCalculator - setStartDateTime(String start)

Sets the start date and time for the duration calculations.

Parameters

Name Type Description

Display value for


the start time in
start String
GMT for subsequent
calculations.

PDF generated on August 20, 2023 15


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

Returns

Type Description

void

Example

var dc = new DurationCalculator();


dc.setStartDateTime("2020-04-10 08:00:00")

DurationCalculator - setStartDateTime(GlideDateTime description)

Sets the start date and time for the duration calculations.

Parameters

Name Type Description

GlideDateTime object
that contains the
description GlideDateTime start time in
GMT for subsequent
calculations.

Returns

Type Description

void

Example

var startDateTime = new GlideDateTime("2020-11-02 11:00:00


");
var dur = new DurationCalculator();

// Set 9-5 weekday schedule.


dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
dur.setStartDateTime(startDateTime);

PDF generated on August 20, 2023 16


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.
Vancouver API Reference

DurationCalculator - setTimeZone(String timezone)

Sets the time zone to use for calculating the due date.

Parameters

Name Type Description

Value of the time


timezone String
zone.

Returns

Type Description

void

Example

var dc = new DurationCalculator();


dc.setTimeZone("Los Angeles");

PDF generated on August 20, 2023 17


©2023 ServiceNow. All rights reserved. Terms of Use Privacy Statement

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the
United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are
associated.

You might also like