{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":990,"path":"api-reference/server-api-reference/c_DurationCalculatorAPI.md","title":"DurationCalculator - Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# DurationCalculator- Global\n\nThe DurationCalculator script include provides methods for calculating durations and due dates.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## DurationCalculator - DurationCalculator\\( \\)\n\nConstructor for DurationCalculator class.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n```\nvar dc = new DurationCalculator();\n```\n\n## DurationCalculator - calcDuration\\(Number seconds\\)\n\nCalculates an end date and time based on current start date and time and passed in duration \\(seconds\\).\n\nThis 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.\n\nPrior to calling this method, you must call setStartDateTime\\(\\) with the start time to use for the duration.\n\n|Name|Type|Description|\n|----|----|-----------|\n|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.|\n\n|Type|Description|\n|----|-----------|\n|Boolean|False if the input value is not a number.|\n\n```\nvar gdt = new GlideDateTime(\"2020-05-01 00:00:00\");\n\nvar dc = new DurationCalculator();\ndc.setStartDateTime(gdt);\n\nif(!dc.calcDuration(2*24*3600)){ // 2 days\n  gs.log(\"*** Error calculating duration\");\n  return;\n}\nvar secs = dc.getSeconds();\nvar totalSecs = dc.getTotalSeconds();\n\ngs.print(\"***SCHEDULE DURATION: SECS=\" + secs + \" TOTALSECS=\" + totalSecs + \" ENDTIME = \" + endDateTime);\n```\n\nOutput\n\n```\n***SCHEDULE DURATION: SECS=172800 TOTALSECS=970534 ENDTIME = 2020-05-03 00:00:00\n```\n\n## DurationCalculator - calcRelativeDueDate\\(GlideDateTime start, Number days, String endTime\\)\n\nCalculates the due date starting at the passed in start time and adding the number of days using the current schedule and time zone.\n\nCalled 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.\n\n<table id=\"table_vw4_gvb_4t\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nstart\n\n</td><td>\n\nGlideDateTime\n\n</td><td>\n\nGlideDateTime object that contains the start date for the computation.\n\n</td></tr><tr><td>\n\ndays\n\n</td><td>\n\nNumber\n\n</td><td>\n\nNumber of days to add to the start date.\n\n</td></tr><tr><td>\n\nendTime\n\n</td><td>\n\nString\n\n</td><td>\n\nTime of day that the work is due on the computed due date.If blank, defaults to the end of the work day.\n\n Format: HH:mm:ss\n\n</td></tr></tbody>\n</table><table id=\"table_ww4_gvb_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates if the completion date is within the schedule.Possible values:\n\n-   true: Completion date is within the schedule.\n-   false: Completion date falls outside the schedule. Undefined if no schedule was set prior to calling this method.\n\n</td></tr></tbody>\n</table>```\nvar dc = new DurationCalculator();\nvar startTime = new GlideDateTime();\n\n// Settings for calculations\n// Optional: Specify the schedule to use for the following calculations\ndc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); \n// Optional: Specify a different timezone to use\ndc.setTimeZone(\"Los Angeles\");\n// Optional: Set a start date and time, otherwise the current time is assumed\ndc.setStartDateTime(\"2020-04-10 08:00:00\")\n// Calculate end time, from number of seconds required in the schedule\ndc.calcDuration(3*24*3600); // 3 days\ndc.calcRelativeDueDate(startTime, \"3\", \"07:00:00\");\n```\n\n## DurationCalculator - calcRelativeDuration\\(String relativeDurationID\\)\n\nCalculates the duration using the specified relative duration script.\n\nUpon completion, the **this.endDateTime** and **this.seconds** properties are set to indicate the results of the calculation.\n\n|Name|Type|Description|\n|----|----|-----------|\n|relativeDurationID|String|sys\\_id of relative duration schedule \\(table cmn\\_relative\\_duration\\).|\n\n|Type|Description|\n|----|-----------|\n|Boolean|The result of the duration script.|\n\n```\nvar dc = new DurationCalculator();\ndc.calcRelativeDuration('08fcd0830a0a1b2600074f56b1ad7cb');\n```\n\n## DurationCalculator - calcScheduleDuration\\(String startTime, String endTime\\)\n\nReturns the duration between the **startTime** and the **endTime** within the already-specified schedule and optionally overridden timezone.\n\nThis method also sets **this.endDateTime**, **this.seconds**, and **this.totalSeconds** in the current schedule object.\n\n<table id=\"table_lw4_gvb_4t\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nstartTime\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. 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);`.\n\n</td></tr><tr><td>\n\nendTime\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Display value for the start time.Default: Uses the current date and time \\(set using setEndDateTime\\(\\)\\).\n\n</td></tr></tbody>\n</table><table id=\"table_mw4_gvb_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nNumber\n\n</td><td>\n\nSchedule duration. Returns 0 if **endTime** is before **startTime**.Unit: Seconds\n\n</td></tr></tbody>\n</table>```\nvar dur = new DurationCalculator();\n\n// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set\ndur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae');\ndur.calcScheduleDuration(\"2019-01-02 11:00:00\", \"2019-01-06 09:00:00\");\nvar secs = dur.getSeconds();\nvar totalSecs = dur.getTotalSeconds();\nvar endDateTime = dur.getEndDateTime() + \"\";\n\ngs.print(\"***SCHEDULE DURATION: SECS=\" + secs + \" TOTALSECS=\" + totalSecs + \" ENDTIME = \" + endDateTime);\n```\n\nOutput\n\n```\n***SCHEDULE DURATION: SECS=97200 TOTALSECS=338400 ENDTIME = 2019-01-06 09:00:00\n```\n\n## DurationCalculator - calcScheduleDuration\\(GlideDateTime startTime, GlideDateTime endTime\\)\n\nReturns the duration between the **startTime** and the **endTime** within the already-specified schedule and optionally overridden timezone.\n\nThis method also sets **this.endDateTime**, **this.seconds**, and **this.totalSeconds** in the current schedule object.\n\n<table id=\"table_nw4_gvb_4t\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nstartTime\n\n</td><td>\n\nGlideDateTime\n\n</td><td>\n\nOptional. 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);`.\n\n</td></tr><tr><td>\n\nendTime\n\n</td><td>\n\nGlideDateTime\n\n</td><td>\n\nOptional. GlideDateTime object that contains the end time to use. Default: Uses the current date and time \\(set using setEndDateTime\\(\\)\\).\n\n</td></tr></tbody>\n</table><table id=\"table_mw4_gvb_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nNumber\n\n</td><td>\n\nSchedule duration. Returns 0 if **endTime** is before **startTime**.Unit: Seconds\n\n</td></tr></tbody>\n</table>```\nvar startDateTime = new GlideDateTime(\"2020-11-02 11:00:00\");\n// Instantiate a new GlideDateTime object which has the end date as the current date and time\nvar endDateTime = new GlideDateTime(); \nvar dur = new DurationCalculator();\n\n// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set\ndur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); \ndur.calcScheduleDuration(startDateTime, endDateTime);\nvar secs = dur.getSeconds();\nvar totalSecs = dur.getTotalSeconds();\n\ngs.print(\"***SCHEDULE DURATION: SECS=\" + secs + \" TOTALSECS=\" + totalSecs + \" ENDTIME = \" + endDateTime);\n```\n\nOutput\n\n```\n***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34\n```\n\n## DurationCalculator - getEndDateTime\\( \\)\n\nGets the **endDateTime** property that was set by calcDuration/calcRelativeDuration, indicating the end date and time for the duration.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|GlideDateTime|The end datetime.|\n\n```\nvar dc = new DurationCalculator();\ndc.calcDuration(52);\ngs.print(dc.getEndDateTime());\n```\n\n2012-04-17 20:57:27\n\n## DurationCalculator - getSeconds\\( \\)\n\nReturns the **this.seconds** property that was set by calcDuration/calcRelativeDuration, indicating the total number of seconds of work to be performed for the duration.\n\nThis 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|The total work time, in seconds.|\n\n```\nvar startDateTime = new GlideDateTime(\"2020-11-02 11:00:00\");\n// Instantiate a new GlideDateTime object which has the end date as the current date and time\nvar endDateTime = new GlideDateTime(); \nvar dur = new DurationCalculator();\n\n// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set\ndur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); \ndur.calcScheduleDuration(startDateTime, endDateTime);\nvar secs = dur.getSeconds();\nvar totalSecs = dur.getTotalSeconds();\n\ngs.print(\"***SCHEDULE DURATION: SECS=\" + secs + \" TOTALSECS=\" + totalSecs + \" ENDTIME = \" + endDateTime);\n```\n\nOutput\n\n```\n***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34\n```\n\n## DurationCalculator - getTotalSeconds\\( \\)\n\nReturns the **totalSeconds** value that was set by a call to calculate\\(record\\).\n\nThe **totalSeconds** property represents the total number of seconds between the start and end times of the duration, which includes both work and non-work.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|Number|The total number of seconds.|\n\n```\nvar startDateTime = new GlideDateTime(\"2020-11-02 11:00:00\");\n// Instantiate a new GlideDateTime object which has the end date as the current date and time\nvar endDateTime = new GlideDateTime(); \nvar dur = new DurationCalculator();\n\n// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set\ndur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); \ndur.calcScheduleDuration(startDateTime, endDateTime);\nvar secs = dur.getSeconds();\nvar totalSecs = dur.getTotalSeconds();\n\ngs.print(\"***SCHEDULE DURATION: SECS=\" + secs + \" TOTALSECS=\" + totalSecs + \" ENDTIME = \" + endDateTime);\n```\n\nOutput\n\n```\n***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34\n```\n\n## DurationCalculator - isAfter\\(GlideDateTime dt, String tm\\)\n\nCompares the passed in time to the time value in the passed in GlideDateTime object.\n\n<table id=\"table_tw4_gvb_4t\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\ndt\n\n</td><td>\n\nGlideDateTime or String\n\n</td><td>\n\nEither 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.\n\n</td></tr><tr><td>\n\ntm\n\n</td><td>\n\nString\n\n</td><td>\n\nDisplay value for the time to compare against the time value in the GlideDateTime object. Assumes same date.Format: HH:mm:ss\n\n</td></tr></tbody>\n</table><table id=\"table_uw4_gvb_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates if the passed in time value \\(**tm**\\) is after the time in the GlideDateTime object \\(**dt**\\). Possible values:\n\n-   true: **tm** is after **dt**.\n-   false **tm** is not after **dt**.\n\n</td></tr></tbody>\n</table>```\nvar dc = new DurationCalculator();\ngs.print(dc.isAfter(\"2020-04-10 08:00:00\", \"09:00:00\"));\n```\n\n\\*\\*\\* Script: true\n\n## DurationCalculator - setSchedule\\(String schedId, String timezone\\)\n\nSets the schedule and time zone to use for calculating the due date.\n\n<table id=\"table_y5v_nlz_nt\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nschedId\n\n</td><td>\n\nString\n\n</td><td>\n\nSys\\_id of the schedule to set. Table: Schedule \\[cmn\\_schedule\\].\n\n</td></tr><tr><td>\n\ntimezone\n\n</td><td>\n\nString\n\n</td><td>\n\nOptional. Time zone to set.Default: User's time zone.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThe following code example shows how to call this method.\n\n```\nvar startDateTime = new GlideDateTime(\"2020-11-02 11:00:00\");\n// Instantiate a new GlideDateTime object which has the end date as the current date and time\nvar endDateTime = new GlideDateTime(); \nvar dur = new DurationCalculator();\n\n// Set 9-5 weekday schedule. This is the schedule in which endDateTime, seconds, and totalseconds is set\ndur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); \ndur.calcScheduleDuration(startDateTime, endDateTime);\nvar secs = dur.getSeconds();\nvar totalSecs = dur.getTotalSeconds();\n\ngs.print(\"***SCHEDULE DURATION: SECS=\" + secs + \" TOTALSECS=\" + totalSecs + \" ENDTIME = \" + endDateTime);\n```\n\nOutput:\n\n```\n***SCHEDULE DURATION: SECS=293734.24 TOTALSECS=970534 ENDTIME = 2020-11-13 16:35:34\n```\n\n## DurationCalculator - setStartDateTime\\(String start\\)\n\nSets the start date and time for the duration calculations.\n\n|Name|Type|Description|\n|----|----|-----------|\n|start|String|Display value for the start time in GMT for subsequent calculations.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar dc = new DurationCalculator();\ndc.setStartDateTime(\"2020-04-10 08:00:00\")\n```\n\n## DurationCalculator - setStartDateTime\\(GlideDateTime description\\)\n\nSets the start date and time for the duration calculations.\n\n|Name|Type|Description|\n|----|----|-----------|\n|description|GlideDateTime|GlideDateTime object that contains the start time in GMT for subsequent calculations.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar startDateTime = new GlideDateTime(\"2020-11-02 11:00:00\"); \nvar dur = new DurationCalculator();\n\n// Set 9-5 weekday schedule.\ndur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); \ndur.setStartDateTime(startDateTime);\n```\n\n## DurationCalculator - setTimeZone\\(String timezone\\)\n\nSets the time zone to use for calculating the due date.\n\n|Name|Type|Description|\n|----|----|-----------|\n|timezone|String|Value of the time zone.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar dc = new DurationCalculator();\ndc.setTimeZone(\"Los Angeles\");\n```\n\n"}}