---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# DurationCalculator - Global

# DurationCalculator - Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 8 minutes to read

The DurationCalculator script include provides methods for calculating durations and due dates.

## DurationCalculator - DurationCalculator( ) {#ariaid-title2}

Constructor for DurationCalculator class.
{#r_DC-DurationCalculator__table_ik1_1kp_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#r_DC-DurationCalculator__table_ik1_1kp_4t}  

    var dc = new DurationCalculator();

## DurationCalculator - calcDuration(Number seconds) {#ariaid-title3}

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. The seconds property
represents the total number of seconds of work to be performed in the duration based on the work schedule. The totalSeconds property represents the total number of seconds between the start and end times of
the duration, which includes both work and non-work.

Prior to calling this method, you must call setStartDateTime() with the start time to use for the duration.
{#r_DC-calcDuration_N__table_pw4_gvb_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| seconds | Number | Number of seconds to add to the start date and time to compute the end date and time, seconds, and total seconds values. |
[Table 2. Parameters]

{#r_DC-calcDuration_N__table_pw4_gvb_4t} {#r_DC-calcDuration_N__table_qw4_gvb_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | False if the input value is not a number. |
[Table 3. Returns]

{#r_DC-calcDuration_N__table_qw4_gvb_4t}  

    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
      gs.log("*** Error calculating duration");
      return;
    }
    var secs = dc.getSeconds();
    var totalSecs = dc.getTotalSeconds();

    gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

Output

    ***SCHEDULE DURATION: SECS=172800 TOTALSECS=970534 ENDTIME = 2020-05-03 00:00:00

## DurationCalculator - calcRelativeDueDate(GlideDateTime start, Number days,
String endTime) {#ariaid-title4}

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.
{#r_DC-calcRelativeDueDate_GDT_N_S__table_vw4_gvb_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| start | GlideDateTime | GlideDateTime object that contains the start date for the computation. |
| days | Number | Number of days to add to the start date. |
| endTime | String | Time of day that the work is due on the computed due date. If blank, defaults to the end of the work day. Format: HH:mm:ss |
[Table 4. Parameters]

{#r_DC-calcRelativeDueDate_GDT_N_S__table_vw4_gvb_4t} {#r_DC-calcRelativeDueDate_GDT_N_S__table_ww4_gvb_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that indicates if the completion date is within the schedule. Possible values: * true: Completion date is within the schedule. * false: Completion date falls outside the schedule. Undefined if no schedule was set prior to calling this method. |
[Table 5. Returns]

{#r_DC-calcRelativeDueDate_GDT_N_S__table_ww4_gvb_4t}  

    var dc = new DurationCalculator();
    var startTime = new GlideDateTime();

    // Settings for calculations
    // Optional: Specify the schedule to use for the following calculations
    dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); 
    // Optional: Specify a different timezone to use
    dc.setTimeZone("Los Angeles");
    // Optional: Set a start date and time, otherwise the current time is assumed
    dc.setStartDateTime("2020-04-10 08:00:00")
    // Calculate end time, from number of seconds required in the schedule
    dc.calcDuration(3*24*3600); // 3 days
    dc.calcRelativeDueDate(startTime, "3", "07:00:00");

## DurationCalculator - calcRelativeDuration(String relativeDurationID) {#ariaid-title5}

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.
{#r_DC-calcRelativeDuration_S__table_rw4_gvb_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| relativeDurationID | String | sys_id of relative duration schedule (table cmn_relative_duration). |
[Table 6. Parameters]

{#r_DC-calcRelativeDuration_S__table_rw4_gvb_4t} {#r_DC-calcRelativeDuration_S__table_sw4_gvb_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | The result of the duration script. |
[Table 7. Returns]

{#r_DC-calcRelativeDuration_S__table_sw4_gvb_4t}  

    var dc = new DurationCalculator();
    dc.calcRelativeDuration('08fcd0830a0a1b2600074f56b1ad7cb');

## DurationCalculator - calcScheduleDuration(String startTime, String endTime) {#ariaid-title6}

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.
{#r_DC-calcScheduleDuration_S_S__table_lw4_gvb_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| startTime | String | Optional. Display value for the end time. Default: Uses the current date and time (set using setStartDateTime()). You must pass a placeholder if this parameter it not passed, such as `dur.calcScheduleDuration("", endDateTime);`. |
| endTime | String | Optional. Display value for the start time. Default: Uses the current date and time (set using setEndDateTime()). |
[Table 8. Parameters]

{#r_DC-calcScheduleDuration_S_S__table_lw4_gvb_4t} {#r_DC-calcScheduleDuration_S_S__table_mw4_gvb_4t__entry__2}

| Type | Description |
|-|-|
| Number | Schedule duration. Returns 0 if endTime is before startTime. Unit: Seconds |
[Table 9. Returns]

{#r_DC-calcScheduleDuration_S_S__table_mw4_gvb_4t}  

    var dur = new DurationCalculator();

    // Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set
    dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');
    dur.calcScheduleDuration("2019-01-02 11:00:00", "2019-01-06 09:00:00");
    var secs = dur.getSeconds();
    var totalSecs = dur.getTotalSeconds();
    var endDateTime = dur.getEndDateTime() + "";

    gs.print("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

Output

    ***SCHEDULE DURATION: SECS=97200 TOTALSECS=338400 ENDTIME = 2019-01-06 09:00:00

## DurationCalculator - calcScheduleDuration(GlideDateTime startTime, GlideDateTime
endTime) {#ariaid-title7}

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.
{#r_DC-calcScheduleDuration_GDT_GDT__table_nw4_gvb_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| startTime | GlideDateTime | Optional. GlideDateTime object that contains the start time to use. Default: Uses the current date and time (set using setStartDateTime()). You must pass a placeholder if this parameter it not passed, such as `dur.calcScheduleDuration("", endDateTime);`. |
| endTime | GlideDateTime | Optional. GlideDateTime object that contains the end time to use. Default: Uses the current date and time (set using setEndDateTime()). |
[Table 10. Parameters]

{#r_DC-calcScheduleDuration_GDT_GDT__table_nw4_gvb_4t} {#r_DC-calcScheduleDuration_GDT_GDT__table_mw4_gvb_4t__entry__2}

| Type | Description |
|-|-|
| Number | Schedule duration. Returns 0 if endTime is before startTime. Unit: Seconds |
[Table 11. Returns]

{#r_DC-calcScheduleDuration_GDT_GDT__table_mw4_gvb_4t}  

    var startDateTime = new GlideDateTime("2020-11-02 11:00:00");
    // Instantiate a new GlideDateTime object which has the end 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 which 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 + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

Output

    ***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34

## DurationCalculator - getEndDateTime( ) {#ariaid-title8}

Gets the endDateTime property that was set by
calcDuration/calcRelativeDuration, indicating the end
date and time for the duration.
{#r_DC-getEndDateTime__table_gvv_nlz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 12. Parameters]

{#r_DC-getEndDateTime__table_gvv_nlz_nt} {#r_DC-getEndDateTime__table_hvv_nlz_nt__entry__2}

| Type | Description |
|-|-|
| GlideDateTime | The end datetime. |
[Table 13. Returns]

{#r_DC-getEndDateTime__table_hvv_nlz_nt}  

    var dc = new DurationCalculator();
    dc.calcDuration(52);
    gs.print(dc.getEndDateTime());

2012-04-17 20:57:27

## DurationCalculator - getSeconds( ) {#ariaid-title9}

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.
{#r_DC-getSeconds__table_ivv_nlz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 14. Parameters]

{#r_DC-getSeconds__table_ivv_nlz_nt} {#r_DC-getSeconds__table_jvv_nlz_nt__entry__2}

| Type | Description |
|-|-|
| Number | The total work time, in seconds. |
[Table 15. Returns]

{#r_DC-getSeconds__table_jvv_nlz_nt}  

    var startDateTime = new GlideDateTime("2020-11-02 11:00:00");
    // Instantiate a new GlideDateTime object which has the end 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 which 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 + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

Output

    ***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34

## DurationCalculator - getTotalSeconds( ) {#ariaid-title10}

Returns the totalSeconds value that was set by a call to
calculate(record).
The totalSeconds property represents the total number of seconds between the start and end times of the duration, which includes both work and non-work.
{#r_DC-getTotalSeconds__table_qjq_bs1_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 16. Parameters]

{#r_DC-getTotalSeconds__table_qjq_bs1_4t} {#r_DC-getTotalSeconds__table_rjq_bs1_4t__entry__2}

| Type | Description |
|-|-|
| Number | The total number of seconds. |
[Table 17. Returns]

{#r_DC-getTotalSeconds__table_rjq_bs1_4t}  

    var startDateTime = new GlideDateTime("2020-11-02 11:00:00");
    // Instantiate a new GlideDateTime object which has the end 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 which 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 + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

Output

    ***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34

## DurationCalculator - isAfter(GlideDateTime dt, String tm) {#ariaid-title11}

Compares the passed in time to the time value in the passed in GlideDateTime
object.
{#r_DC-isAfter_GDT_S__table_tw4_gvb_4t__entry__3}

| Name | Type | Description |
|-|-|-|
| dt | GlideDateTime or String | Either a GlideDateTime object or a display value date and time to compare to the passed in tm value. If you pass a display value date and time, the method converts it to a GlideDateTime object. |
| tm | String | Display value for the time to compare against the time value in the GlideDateTime object. Assumes same date. Format: HH:mm:ss |
[Table 18. Parameters]

{#r_DC-isAfter_GDT_S__table_tw4_gvb_4t} {#r_DC-isAfter_GDT_S__table_uw4_gvb_4t__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that indicates if the passed in time value (tm) is after the time in the GlideDateTime object (dt). Possible values: * true: tm is after dt. * false tm is not after dt. |
[Table 19. Returns]

{#r_DC-isAfter_GDT_S__table_uw4_gvb_4t}  

    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) {#ariaid-title12}

Sets the schedule and time zone to use for calculating the due date.
{#r_DC-setSchedule_S_S__table_y5v_nlz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| schedId | String | Sys_id of the schedule to set. Located in the Schedule \[cmn_schedule\] table. |
| timezone | String | Optional. Time zone to set. Default: User's time zone. |
[Table 20. Parameters]

{#r_DC-setSchedule_S_S__table_y5v_nlz_nt} {#r_DC-setSchedule_S_S__table_z5v_nlz_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 21. Returns]

{#r_DC-setSchedule_S_S__table_z5v_nlz_nt}  

    var startDateTime = new GlideDateTime("2020-11-02 11:00:00");
    // Instantiate a new GlideDateTime object which has the end 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 which 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 + " TOTALSECS=" + totalSecs + " ENDTIME = " + endDateTime);

Output

    ***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34

## DurationCalculator - setStartDateTime(String start) {#ariaid-title13}

Sets the start date and time for the duration calculations.
{#r_DC-setStartDateTime_S__table_cvv_nlz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| start | String | Display value for the start time in GMT for subsequent calculations. |
[Table 22. Parameters]

{#r_DC-setStartDateTime_S__table_cvv_nlz_nt} {#r_DC-setStartDateTime_S__table_dvv_nlz_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 23. Returns]

{#r_DC-setStartDateTime_S__table_dvv_nlz_nt}  

    var dc = new DurationCalculator();
    dc.setStartDateTime("2020-04-10 08:00:00")

## DurationCalculator - setStartDateTime(GlideDateTime description) {#ariaid-title14}

Sets the start date and time for the duration calculations.
{#r_DC-setStartDateTime_GDT_N__table_evv_nlz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| description | GlideDateTime | GlideDateTime object that contains the start time in GMT for subsequent calculations. |
[Table 24. Parameters]

{#r_DC-setStartDateTime_GDT_N__table_evv_nlz_nt} {#r_DC-setStartDateTime_GDT_N__table_fvv_nlz_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 25. Returns]

{#r_DC-setStartDateTime_GDT_N__table_fvv_nlz_nt}  

    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);

## DurationCalculator - setTimeZone(String timezone) {#ariaid-title15}

Sets the time zone to use for calculating the due date.
{#r_DC-setTimeZone_S__table_avv_nlz_nt__entry__3}

| Name | Type | Description |
|-|-|-|
| timezone | String | Value of the time zone. |
[Table 26. Parameters]

{#r_DC-setTimeZone_S__table_avv_nlz_nt} {#r_DC-setTimeZone_S__table_bvv_nlz_nt__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 27. Returns]

{#r_DC-setTimeZone_S__table_bvv_nlz_nt}  

    var dc = new DurationCalculator();
    dc.setTimeZone("Los Angeles");


