<h2>GlideDate and GlideDateTime examples</h2><br/><div style="overflow-x:auto"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta content="text/html; charset=UTF-8" /><meta name="copyright" content="(C) Copyright 2025" /><meta name="DC.rights.owner" content="(C) Copyright 2025" /><meta name="generator" content="DITA-OT" /><meta name="DC.type" content="topic" /><meta name="DC.title" content="Glide Server APIs" /><meta name="abstract" content="ServiceNow provides APIs for the Glide Server." /><meta name="description" content="ServiceNow provides APIs for the Glide Server." /><meta name="DC.subject" content="APIs, Glide Server, Glide Server APIs, GlideAggregate, GlideRecord, GlideRecordSecure, GlideSystem, GlideDateTime, Server API, GlideDate API, GlideDateTime API, Duration Field Value, setting" /><meta name="keywords" content="APIs, Glide Server, Glide Server APIs, GlideAggregate, GlideRecord, GlideRecordSecure, GlideSystem, GlideDateTime, Server API, GlideDate API, GlideDateTime API, Duration Field Value, setting" /><meta name="DC.relation" scheme="URI" content="../../../script/server-scripting/concept/c_ServerScripting.html" /><meta name="DC.relation" scheme="URI" content="../../../build/applications/concept/api-implementation-reference.html" /><meta name="DC.relation" scheme="URI" content="../../../build/applications/concept/api-implementation.html" /><meta name="DC.relation" scheme="URI" content="../../../script/topic/c_Script.html" /><meta name="DC.creator" content="ServiceNow" /><meta name="DC.date.created" content="2023-08-03" /><meta name="DC.date.modified" content="2024-02-01" /><meta name="DC.format" content="XHTML" /><meta name="DC.identifier" content="p_GlideServerAPIs" /><link rel="stylesheet" type="text/css" href="../../../CSS/commonltr.css" /><title>Glide Server APIs</title></head><body> <div class="nested0" id="p_GlideServerAPIs"> <h1 class="title topictitle1" id="ariaid-title1">Glide Server APIs</h1> <div class="body"><p class="shortdesc"><span class="ph">ServiceNow</span> provides APIs for the Glide Server.</p> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../script/server-scripting/concept/c_ServerScripting.html" title="Server scripts run on the server or database. They can change the appearance or behavior of ServiceNow or run as business rules when records and tables are accessed or modified.">Server-side scripting</a></div> </div> </div><div class="topic concept nested1" id="c_GlideAggregate"> <h2 class="title topictitle2" id="ariaid-title2">GlideAggregate</h2> <div class="body conbody"><p class="shortdesc">The <span class="keyword apiname">GlideAggregate</span> class is an extension of <span class="keyword apiname">GlideRecord</span> and allows database aggregation (COUNT, SUM, MIN, MAX, AVG) queries to be done. This can be helpful in creating customized reports or in calculations for calculated fields.</p> <div class="p"><div class="note"><span class="notetitle">Note:</span> This functionality requires a knowledge of JavaScript.</div> </div> <p class="p">For additional information, refer to <a class="xref" href="../../../app-store/dev_portal/API_reference/glideAggregateScoped/concept/c_GlideAggregateScopedAPI.html" title="GlideAggregate enables creating database aggregation queries.">GlideAggregate</a> API.</p> </div> <div class="topic reference nested2" id="r_GlideAggregateExamples"> <h3 class="title topictitle3" id="ariaid-title3">GlideAggregate examples</h3> <div class="body refbody"><p class="shortdesc"><span class="keyword apiname">GlideAggregate</span> is an extension of <span class="keyword apiname">GlideRecord</span> and its use is probably best shown through a series of examples.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> This functionality requires a knowledge of JavaScript.</div> </div> <p class="p">Here is an example that simply gets a count of the number of records in a table:</p> <div class="p"><pre class="pre codeblock"><code>var count = new GlideAggregate('incident'); count.addAggregate('COUNT'); count.query(); var incidents = 0; if(count.next()) incidents = count.getAggregate('COUNT');</code></pre></div> <p class="p">There is no query associated with the preceding example. If you want to get a count of the incidents that were open, simply add a query as is done with <span class="keyword apiname">GlideRecord</span>. Here is an example to get a count of the number of active incidents.</p> <div class="p"><pre class="pre codeblock"><code>var count = new GlideAggregate('incident'); count.addQuery('active','true'); count.addAggregate('COUNT'); count.query(); var incidents = 0; if(count.next()) incidents = count.getAggregate('COUNT');</code></pre></div> <p class="p">To get a count of all the open incidents by category the code is:</p> <div class="p"><pre class="pre codeblock"><code>var count = new GlideAggregate('incident'); count.addQuery('active','true'); count.addAggregate('COUNT','category'); count.query(); while(count.next()){ var category = count.category; var categoryCount = count.getAggregate('COUNT','category'); gs.log("The are currently "+ categoryCount +" incidents with a category of "+ category);}</code></pre></div> <p class="p">The output is:</p> <div class="p"><pre class="pre codeblock"><code> *** Script: The are currently 1.0 incidents with a category of Data *** Script: The are currently 11.0 incidents with a category of Enhancement *** Script: The are currently 1.0 incidents with a category of Implementation *** Script: The are currently 197.0 incidents with a category of inquiry *** Script: The are currently 13.0 incidents with a category of Issue *** Script: The are currently 1.0 incidents with a category of *** Script: The are currently 47.0 incidents with a category of request</code></pre></div> <p class="p">The following is an example that uses multiple aggregations to see how many times records have been modified using the <var class="keyword varname">MIN</var>, <var class="keyword varname">MAX</var>, and <var class="keyword varname">AVG</var> values.</p> <div class="p"><pre class="pre codeblock"><code>var count = new GlideAggregate('incident'); count.addAggregate('MIN','sys_mod_count'); count.addAggregate('MAX','sys_mod_count'); count.addAggregate('AVG','sys_mod_count'); count.groupBy('category'); count.query(); while(count.next()){ var min = count.getAggregate('MIN','sys_mod_count'); var max = count.getAggregate('MAX','sys_mod_count'); var avg = count.getAggregate('AVG','sys_mod_count'); var category = count.category.getDisplayValue(); gs.log(category +" Update counts: MIN = "+ min +" MAX = "+ max +" AVG = "+ avg);}</code></pre></div> <p class="p">The output is:</p> <div class="p"><pre class="pre codeblock"><code> *** Script: Data Import Update counts: MIN = 4.0 MAX = 21.0 AVG = 9.3333 *** Script: Enhancement Update counts: MIN = 1.0 MAX = 44.0 AVG = 9.6711 *** Script: Implementation Update counts: MIN = 4.0 MAX = 8.0 AVG = 6.0 *** Script: inquiry Update counts: MIN = 0.0 MAX = 60.0 AVG = 5.9715 *** Script: Inquiry / Help Update counts: MIN = 1.0 MAX = 3.0 AVG = 2.0 *** Script: Issue Update counts: MIN = 0.0 MAX = 63.0 AVG = 14.9459 *** Script: Monitor Update counts: MIN = 0.0 MAX = 63.0 AVG = 3.6561 *** Script: request Update counts: MIN = 0.0 MAX = 53.0 AVG = 5.0987</code></pre></div> <p class="p">The following is a more complex example that shows how to compare activity from one month to the next.</p> <div class="p"><pre class="pre codeblock"><code>var agg = new GlideAggregate('incident'); agg.addAggregate('count','category'); agg.orderByAggregate('count','category'); agg.orderBy('category'); agg.addQuery('opened_at','>=','javascript:gs.monthsAgoStart(2)'); agg.addQuery('opened_at','<=','javascript:gs.monthsAgoEnd(2)'); agg.query(); while(agg.next()){ var category = agg.category; var count = agg.getAggregate('count','category'); var query = agg.getQuery(); var agg2 = new GlideAggregate('incident'); agg2.addAggregate('count','category'); agg2.orderByAggregate('count','category'); agg2.orderBy('category'); agg2.addQuery('opened_at','>=','javascript:gs.monthsAgoStart(3)'); agg2.addQuery('opened_at','<=','javascript:gs.monthsAgoEnd(3)'); agg2.addEncodedQuery(query); agg2.query(); var last =""; while(agg2.next()){ last = agg2.getAggregate('count','category');} gs.log(category +": Last month:"+ count +" Previous Month:"+ last); }</code></pre></div> <p class="p">The output is:</p> <div class="p"><pre class="pre codeblock"><code> *** Script: Monitor: Last month:6866.0 Previous Month:4468.0 *** Script: inquiry: Last month:142.0 Previous Month:177.0 *** Script: request: Last month:105.0 Previous Month:26.0 *** Script: Issue: Last month:8.0 Previous Month:7.0 *** Script: Enhancement: Last month:5.0 Previous Month:5.0 *** Script: Implementation: Last month:1.0 Previous Month:0</code></pre></div> <p class="p">The following is an example to obtain distinct count of a field on a group query.</p> <div class="p"><pre class="pre codeblock"><code>var agg = new GlideAggregate('incident'); agg.addAggregate('count'); agg.addAggregate('count(distinct','category'); agg.addQuery('opened_at', '>=', 'javascript:gs.monthsAgoStart(2)'); agg.addQuery('opened_at', '<=', 'javascript:gs.monthsAgoEnd(2)'); // agg.groupBy('priority'); agg.query(); while (agg.next()) { // Expected count of incidents and count of categories within each priority value (group) gs.info('Incidents in priority ' + agg.priority + ' = ' + agg.getAggregate('count') + ' (' + agg.getAggregate('count(distinct','category') + ' categories)'); }</code></pre></div> <p class="p">The output is:</p> <div class="p"><pre class="pre codeblock"><code>*** Script: Incidents in priority 1 = 13 (3 categories) *** Script: Incidents in priority 2 = 10 (5 categories) *** Script: Incidents in priority 3 = 5 (3 categories) *** Script: Incidents in priority 4 = 22 (6 categories)</code></pre></div> <div class="p">You can implement the SUM aggregate with or without the use of the <span class="keyword apiname">groupBy()</span> method. If you do not use the <span class="keyword apiname">groupBy()</span> method, the result of the SUM is the cumulative value for each different value of the field for which you request the SUM. For example, if you SUM the total_cost field in the Fixed Asset table, and the Fixed Asset table contains 12 total records: <ul class="ul" id="r_GlideAggregateExamples__ul_qsb_pmx_42b"><li class="li">Three records with a total_cost of $12</li><li class="li">Four records with a total_cost of $10</li><li class="li">Five records with a total_cost of $5</li></ul> When you SUM the record set, the <span class="keyword apiname">getAggregate()</span> method returns three different sums: $36, $40, and $25.</div> <p class="p">The following code illustrates implementing the SUM aggregate without using the <span class="keyword apiname">groupBy()</span> method:</p> <div class="p"><pre class="pre codeblock"><code>var totalCostSum = new GlideAggregate('fixed_asset'); totalCostSum.addAggregate('SUM', 'total_cost'); totalCostSum.query(); while (totalCostSum.next()) { var allTotalCost = 0; allTotalCost = totalCostSum.getAggregate('SUM', 'total_cost'); aTotalCost = totalCostSum.getValue('total_cost'); gs.print('Unique field value: ' + aTotalCost + ', SUM = ' + allTotalCost + ', ' + allTotalCost/aTotalCost + ' records'); }</code></pre></div> <p class="p">The output for this example is:</p> <div class="p"><pre class="pre codeblock"><code>*** Script: Unique field value: 12, SUM = 36, 3 records *** Script: Unique field value: 10, SUM = 40, 4 records *** Script: Unique field value: 5, SUM = 25, 5 records</code></pre></div> <p class="p">Using the same data points as the prior example, if you use the <span class="keyword apiname">groupBy()</span> method, the SUM aggregate returns the sum of all values for the specified field.</p> <p class="p">The following example illustrates implementing the SUM aggregate using the <span class="keyword apiname">groupBy()</span> method:</p> <div class="p"><pre class="pre codeblock"><code>var totalCostSum = new GlideAggregate('fixed_asset'); totalCostSum.addAggregate('SUM', 'total_cost'); totalCostSum.groupBy('total_cost'); totalCostSum.query(); if(totalCostSum.next()){ // in case there is no result var allTotalCost = 0; allTotalCost = totalCostSum.getAggregate('SUM', 'total_cost'); gs.print('SUM of total_cost: = ' + allTotalCost); }</code></pre></div> <p class="p">The output for this example is:</p> <div class="p"><pre class="pre codeblock"><code>*** Script: SUM of total_cost: 101</code></pre></div> </div> </div> </div> </div> <div class="topic concept nested1" id="c_GlideRecord"> <h2 class="title topictitle2" id="ariaid-title4">GlideRecord</h2> <div class="body conbody"><p class="shortdesc"><span class="keyword apiname">GlideRecord</span> is a special Java class (<span class="keyword apiname">GlideRecord.java</span>) that can be used in JavaScript exactly as if it was a native JavaScript class.</p> <div class="p"><span class="keyword apiname">GlideRecord</span>:<ul class="ul"><li class="li">is used for database operations instead of writing SQL queries.</li><li class="li">is an object that contains zero or more records from one table. Another way to say this is that a GlideRecord is an ordered list.</li></ul> </div> <p class="p">A GlideRecord contains both records (rows) and fields (columns). The field names are the same as the underlying database column names. For additional information, refer to <a class="xref" href="../../../app-store/dev_portal/API_reference/glideRecordScoped/concept/c_GlideRecordScopedAPI.html" title="Scoped GlideRecord is used for database operations.">GlideRecord - Scoped</a>.</p> <div class="note"><span class="notetitle">Note:</span> Use of <code class="ph codeph">gs.sql())</code> scripting syntax was discontinued in <span class="ph">Geneva</span>. Use standard <span class="keyword apiname">GlideRecord</span> syntax in its place.</div> </div> </div> <div class="topic concept nested1" id="c_UsingGlideRecordSecure"> <h2 class="title topictitle2" id="ariaid-title5">Using GlideRecordSecure</h2> <div class="body conbody"><p class="shortdesc"><span class="keyword apiname">GlideRecordSecure</span> is a class inherited from <span class="keyword apiname">GlideRecord</span> that performs the same functions as <span class="keyword apiname">GlideRecord</span>, and also enforces ACLs.</p> <div class="section"><h3 class="title sectiontitle">Non-writable fields</h3> <p class="p">Be aware that, when using <span class="keyword apiname">GlideRecordSecure</span>, non-writable fields are set to NULL when trying to write to the database. By default, <span class="keyword apiname">canCreate()</span> on the column is replaced with <span class="keyword apiname">canWrite()</span> on the column. If that returns false, the column value is set to NULL.</p> </div> <div class="section"><h3 class="title sectiontitle">Checking for NULL values</h3> <div class="p">If an element cannot be read because an ACL restricts access, a NULL value is created in memory for that record. With GlideRecord, you must explicitly check for any ACLs that might restrict read access to the record. To do so, an if statement such as the following is required to check if the record can be read:<pre class="pre codeblock"><code>if ( !grs.canRead() ) continue;</code></pre></div> <div class="p">With <span class="keyword apiname">GlideRecordSecure</span>, you do not need to explicitly check for read access using <span class="keyword apiname">canRead()</span>. Instead, you can use <span class="keyword apiname">next()</span> by itself to move to the next record. The following example provides a comparison between <span class="keyword apiname">GlideRecord</span> and <span class="keyword apiname">GlideRecordSecure</span>. <pre class="pre codeblock"><code>var count = 0; var now_GR = new GlideRecord('mytable'); now_GR. query(); while (now_GR. next()) { if (!now_GR. canRead()) continue; if (!now_GR. canWrite()) continue; if (!now_GR. val. canRead() || !now_GR. val. canWrite()) now_GR. val = null; else now_GR. val = "val-" + now_GR. id; if (now_GR. update()) count ++; }</code></pre> <pre class="pre codeblock"><code>var count = 0; var grs = new GlideRecordSecure('mytable'); grs. query(); while (grs. next()) { grs. val = "val-" + grs. id; if (grs. update()) count ++; }</code></pre></div> </div> <div class="section"><h3 class="title sectiontitle">Examples</h3> <p class="p">These are two simple examples using <span class="keyword apiname">GlideRecordSecure</span>.</p> <div class="p"><pre class="pre codeblock"><code>var att = new GlideRecordSecure ('sys_attachment'); att. get('$[sys_attachment.sys_id]'); var sm = GlideSecurityManager.get(); var checkMe = 'record/sys_attachment/delete'; var canDelete = sm.hasRightsTo(checkMe,att); gs. log('canDelete: ' + canDelete); canDelete;</code></pre></div> <div class="p"><pre class="pre codeblock"><code>var grs = new GlideRecordSecure('task_ci'); grs.addQuery(); grs.query(); var count = grs. getRowCount(); if (count > 0 ) { var allocation = parseInt(10000/count) / 100; while (grs.next()) { grs.u_allocation = allocation; grs.update(); } }</code></pre></div> </div> </div> </div> <div class="topic concept nested1" id="c_GlideSystem"> <h2 class="title topictitle2" id="ariaid-title6">GlideSystem</h2> <div class="body conbody"><p class="shortdesc">The <span class="keyword apiname">GlideSystem</span> API provides methods for retrieving information.</p> <p class="p">The <span class="keyword apiname">GlideSystem</span> (referred to by the variable name '<var class="keyword varname">gs</var>' in business rules) provides a number of convenient methods to get information about the system, the current logged in user, etc. For example, the method <span class="keyword apiname">addInfoMessage()</span> permits communication with the user.</p> <pre class="pre codeblock"><code> gs.addInfoMessage('Email address added for notification');</code></pre> <p class="p">Many of the <span class="keyword apiname">GlideSystem</span> methods facilitate the easy inclusion of dates in query ranges and are most often used in filters and reporting.</p> <p class="p">For additional information, see <a class="xref" href="../../../app-store/dev_portal/API_reference/glideSystemScoped/concept/c_GlideSystemScopedAPI.html" title="The scoped GlideSystem (referred to by the variable name 'gs' in any server-side JavaScript) API provides a number of convenient methods to get information about the system, the current logged in user, etc.">GlideSystem</a>.</p> </div> </div> <div class="topic concept nested1" id="c_GlideDateTime"> <h2 class="title topictitle2" id="ariaid-title7">GlideDateTime</h2> <div class="body conbody"><p class="shortdesc">The <span class="keyword apiname">GlideDateTime</span> class provides methods for performing operations on <span class="keyword apiname">GlideDateTime</span> objects, such as instantiating <span class="keyword apiname">GlideDateTime</span> objects or working with <var class="keyword varname">glide_date_time</var> fields.</p> <p class="p">In addition to the instantiation methods described below, a GlideDateTime object can be instantiated from a <var class="keyword varname">glide_date_time</var> field using the <span class="keyword apiname">getGlideObject()</span> method (for example, <code class="ph codeph">var gdt = gr.my_datetime_field.getGlideObject();</code>).</p> <p class="p">Some methods use the Java Virtual Machine time zone when retrieving or modifying a date and time value. Using these methods may result in unexpected behavior. Use equivalent local time and UTC methods whenever possible.</p> </div> <div class="related-links"> <div class="linklist relinfo relconcepts"><strong>Related concepts</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../../app-store/dev_portal/API_reference/GlideDate/concept/GlideDateAPI.html" title="The GlideDate class provides methods for performing operations on GlideDate objects, such as instantiating GlideDate objects or working with GlideDate fields.">GlideDate</a></li><li class="linklist"><a class="link" href="../../../app-store/dev_portal/API_reference/glideDateScoped/concept/c_GlideDateScopedAPI.html" title="The scoped GlideDate class provides methods for performing operations on GlideDate objects, such as instantiating GlideDate objects or working with GlideDate fields.">GlideDate</a></li><li class="linklist"><a class="link" href="../../../app-store/dev_portal/API_reference/GlideDateTime/concept/c_GlideDateTimeAPI.html" title="The GlideDateTime class provides methods for performing operations on GlideDateTime objects, such as instantiating GlideDateTime objects or working with glide_date_time fields.">GlideDateTime</a></li><li class="linklist"><a class="link" href="../../../app-store/dev_portal/API_reference/glideDateTimeScoped/concept/c_GlideDateTimeScoped.html" title="The scoped GlideDateTime class provides methods for performing operations on GlideDateTime objects, such as instantiating GlideDateTime objects or working with glide_date_time fields.">GlideDateTime</a></li><li class="linklist"><a class="link" href="../../../app-store/dev_portal/API_reference/glideTimeScoped/concept/c_GlideTimeScopedAPI.html" title="The scoped GlideTime class provides methods for performing operations on GlideTime objects, such as instantiating GlideTime objects or working with GlideTime fields.">GlideTime</a></li></ul></div> <div class="linklist relinfo relref"><strong>Related reference</strong><br /> <ul class="linklist"><li class="linklist"><a class="link" href="../../useful-scripts/concept/useful-server-side-scripts.html#r_ModifyAGlideDateTimeFieldValue" title="This example demonstrates how to modify a GlideDateTime field value using a server-side script.">Modify a GlideDateTime field value</a></li></ul></div> </div> <div class="topic reference nested2" id="r_GlideDate_GlideDateTime_examples"> <h3 class="title topictitle3" id="ariaid-title8">GlideDate and GlideDateTime examples</h3> <div class="body refbody"><p class="shortdesc">The <span class="keyword apiname">GlideDate</span> and <span class="keyword apiname">GlideDateTime</span> APIs are used to manipulate date and time values.</p> <div class="section"> <div class="p"><div class="note"><span class="notetitle">Note:</span> This functionality requires a knowledge of JavaScript.</div> </div> <p class="p">For additional information, refer to <a class="xref" href="../../../app-store/dev_portal/API_reference/glideDateScoped/concept/c_GlideDateScopedAPI.html" title="The scoped GlideDate class provides methods for performing operations on GlideDate objects, such as instantiating GlideDate objects or working with GlideDate fields.">GlideDate</a> API and <a class="xref" href="../../../app-store/dev_portal/API_reference/glideDateTimeScoped/concept/c_GlideDateTimeScoped.html" title="The scoped GlideDateTime class provides methods for performing operations on GlideDateTime objects, such as instantiating GlideDateTime objects or working with glide_date_time fields.">GlideDateTime</a> API.</p> <div class="p">You can create a <span class="keyword apiname">GlideDateTime</span> object from a <span class="keyword apiname">GlideDate</span> object by passing in the <span class="keyword apiname">GlideDate</span> object as a parameter to the <span class="keyword apiname">GlideDateTime</span> constructor. By default, the <span class="keyword apiname">GlideDateTime</span> object is expressed in the internal format, yyyy-MM-dd HH:mm:ss and the system time zone UTC.<pre class="pre codeblock"><code>var gDate = new GlideDate(); gDate.setValue('2015-01-01'); gs.info(gDate); var gDT = new GlideDateTime(gDate); gs.info(gDT);</code></pre> Output: <pre class="pre codeblock"><code>2015-01-01 2015-01-01 00:00:00</code></pre></div> <div class="p">See also: <ul class="ul" id="r_GlideDate_GlideDateTime_examples__ul_tbs_4sr_fzb"><li class="li"><a class="xref" href="../../useful-scripts/concept/useful-server-side-scripts.html#r_ModifyAGlideDateTimeFieldValue" title="This example demonstrates how to modify a GlideDateTime field value using a server-side script.">Modify a GlideDateTime field value</a></li><li class="li"><a class="xref" href="../../../app-store/dev_portal/API_reference/glideTimeScoped/concept/c_GlideTimeScopedAPI.html" title="The scoped GlideTime class provides methods for performing operations on GlideTime objects, such as instantiating GlideTime objects or working with GlideTime fields.">GlideTime</a></li></ul> </div> </div> </div> </div> <div class="topic concept nested2" id="c_SettingTheDurationFieldValue"> <h3 class="title topictitle3" id="ariaid-title9">Set a duration field value in script</h3> <div class="body conbody"><p class="shortdesc">Examples of JavaScript that can be used to set the value of a duration field.</p> <div class="p"><div class="note"><span class="notetitle">Note:</span> Negative duration values are not supported.</div> </div> <div class="section"><h4 class="title sectiontitle">Using the GlideDateTime.subtract() method</h4> <div class="p">The <span class="keyword apiname">subtract(GlideDateTime start, GlideDateTime end)</span> method in <a class="xref" href="../../../app-store/dev_portal/API_reference/glideDateTimeScoped/concept/c_GlideDateTimeScoped.html" title="The scoped GlideDateTime class provides methods for performing operations on GlideDateTime objects, such as instantiating GlideDateTime objects or working with glide_date_time fields.">GlideDateTime</a> 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:<pre class="pre codeblock"><code>var duration = GlideDateTime.subtract(start, end);</code></pre></div> <div class="p">If you want to work with the value returned as a number to use in date or duration arithmetic, convert the return to milliseconds:<pre class="pre codeblock"><code>var time = GlideDateTime.subtract(start,end).getNumericValue(); </code></pre></div> <div class="p">If you want to set a duration to the amount of time between some event and the current date/time:<pre class="pre codeblock"><code><duration_field> = GlideDateTime.subtract(new GlideDateTime(<start_time>.getValue()),gs.nowDateTime());</code></pre></div> <p class="p">The time values presented to <span class="keyword apiname">GlideDateTime.subtract</span> are expected to be in the user's time zone and in the user's format.</p> </div> <div class="section"><h4 class="title sectiontitle">Setting a default value of a duration field</h4> <p class="p">Setting the default value for a duration field is similar to the method used in the previous topic.</p> </div> <div class="section"><h4 class="title sectiontitle">Setting the duration field value in a client script</h4> <div class="p">This script sets a <var class="keyword varname">duration_field</var> value in a client script. Replace <var class="keyword varname">duration_field</var> with the field name from your instance.<pre class="pre codeblock"><code>g_form.setValue('<duration_field>','11 01:02:03');</code></pre></div> </div> <div class="section"><h4 class="title sectiontitle">Calculating and setting a duration using a client script</h4> <p class="p">Here is an example of how to return a value and populate it using a client script.</p> <div class="p">Create an <code class="ph codeph">onChange</code> client script that includes the following code. You can modify this script if you need the calculation to happen in an <code class="ph codeph">onLoad</code> script or some other way.<pre class="pre codeblock"><code>function onChange(control, oldValue, newValue, isLoading){ var strt = g_form.getValue('<start_field>'); var end = g_form.getValue('<end_field>'); 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('<duration_field>', answer);}</code></pre></div> <div class="p">Create a system script include file called <var class="keyword varname">AjaxDurCalc</var> that handles the request. It may be reused for other functions as well.<pre class="pre codeblock"><code>var AjaxDurCalc = Class.create(); AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor,{ durCalc:function(){return GlideDuration.subtract(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'));}});</code></pre></div> </div> <div class="section"><h4 class="title sectiontitle">Changing the duration field value</h4> <div class="p">If you manipulate a duration value with addition/subtraction of some amount of time, 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. The following is an example that adds 11 seconds to the <var class="keyword varname">duration</var> field in the current record.<pre class="pre codeblock"><code>var timems = current.duration.dateNumericValue(); timems = timems + 11*1000; current.duration.setDateNumericValue(timems);</code></pre></div> </div> <div class="section"><h4 class="title sectiontitle">Formatting the Resolve Time</h4> <div class="p">To format the <span class="ph uicontrol">Resolve Time</span> or the <span class="ph uicontrol">Business Resolve Time</span> fields as durations, which displays them as a duration instead of a large integer, add the following attribute to those fields:<pre class="pre codeblock"><code>format=glide_duration</code></pre></div> <p class="p">Modify the dictionary entry for the field and add the attribute. If there is an existing attribute, separate multiple attributes with commas.</p> </div> <div class="section"><h4 class="title sectiontitle">Setting the maximum unit of measurement</h4> <div class="p">The <var class="keyword varname">max_unit</var> dictionary attribute defines the maximum unit of time used in a duration. For example, if <code class="ph codeph">max_unit=minutes</code>, a duration of 3 hours 5 minutes 15 seconds appears as 185 minutes 15 seconds. To set the maximum unit of duration measurement, add the following dictionary attribute to the <var class="keyword varname">duration</var> field:<pre class="pre codeblock"><code>max_unit=<unit></code></pre></div> </div> </div> </div> <div class="topic reference nested2" id="date-and-time-format-guidelines"> <h3 class="title topictitle3" id="ariaid-title10">Date and time format guidelines</h3> <div class="body refbody"><p class="shortdesc">You can specify a date format with a sequence of specific date and time pattern strings. A pattern string consists of one or more uppercase and lowercase letters from A to Z. Any text within quotation marks is ignored and is instead copied into the date output.</p> <div class="section" id="date-and-time-format-guidelines__id_wjx_qtq_fzb"> <div class="p"> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="date-and-time-format-guidelines__tbl_custom-date-formats" class="table" frame="border" border="1" rules="all"><colgroup><col /><col /><col /><col /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1145">String</th><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1148">Description</th><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1151">Output Format</th><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1154">Example</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">G</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Era designator</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Text</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">AD</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">y</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Year</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Year</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">2019; 19</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">Y</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Week in year</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Year</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">2019; 19</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">M</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Month in year (within date)</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Month</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">July; Jul; 07</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">L</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Month in year (standalone value)</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Month</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">July; Jul; 07</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">w</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Week in year</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">52</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">W</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Week in month</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">1</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">D</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Day in year</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">365</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">d</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Day in month</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">2</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">F</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Day of week in month</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">3</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">E</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Day name in week</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Text</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">Wednesday; Wed</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">u</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Day number of week</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">3</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">a</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">a.m. or p.m.</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Text</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">p.m.</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">H</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Hour in day from 0 through 23</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">0</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">k</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Hour in day from 1 through 24</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">24</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">K</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Hour in a.m. or p.m. from 0 through 11</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">0</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">h</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Hour in a.m. or p.m. from 1 through 12</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">12</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">m</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Minute in hour</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">59</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">s</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Second in minute</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">1</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">S</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Millisecond</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Number</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">500</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">z</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Time zone in default format</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Time zone in default format</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">Pacific Standard Time; PST</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">Z</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Time zone in RFC 822 format</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Time zone in RFC 822 format</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">-0800</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1145 ">X</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1148 ">Time zone in ISO 8601 format</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1151 ">Time zone in ISO 8601 format</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1154 ">-08; -0800; -08:00</td></tr></tbody></table> </div> </div> </div> <div class="section" id="date-and-time-format-guidelines__id_b4t_jwk_fzb"><h4 class="title sectiontitle">Common date-time format conflicts</h4> <p class="p">Providing an incorrect date-time format string can result in unexpected behavior for methods in the <span class="keyword apiname">GlideDate</span>, <span class="keyword apiname">GlideDateTime</span>, and <span class="keyword apiname">GlideTime</span> APIs. Use the following table for help with resolving format issues.</p> <div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="date-and-time-format-guidelines__table_gdt_formatGuide" class="table" frame="border" border="1" rules="all"><colgroup><col style="width:33.33333333333333%" /><col style="width:33.33333333333333%" /><col style="width:33.33333333333333%" /></colgroup><thead class="thead" style="text-align:left;"><tr class="row"><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1545">Incorrect format</th><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1548">Explanation</th><th class="entry cellrowborder" style="vertical-align:top;" id="d224339e1551">Correct format</th></tr></thead><tbody class="tbody"><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1545 ">YYYY-MM-dd HH:mm:ss</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1548 "><ul class="ul" id="date-and-time-format-guidelines__ul_blrg_icts_sim"><li class="li">Problem: Using upper case 'Y' to represent the year. The 'Y' symbol is rarely used and represents the week year.</li><li class="li">Solution: Use the lower case 'y' symbol for the calendar year.</li></ul> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1551 ">yyyy-MM-dd HH:mm:ss</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1545 ">HH:mm:ss a<p class="p">hh:mm:ss</p> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1548 "><ul class="ul" id="date-and-time-format-guidelines__ul_q23_f3k_fzb"><li class="li">Problem: Usage conflict between 'HH' and 'hh' symbols. <div class="note"><span class="notetitle">Note:</span> The 'a' symbol, an AM/PM marker, isn’t compatible with H or k (24-hour) symbols.</div> </li><li class="li">Solution: Use the proper configuration to represent the hour format.<ul class="ul" id="date-and-time-format-guidelines__ul_ycp_q4k_fzb"><li class="li">HH denotes 24-hour time format with a start count of zero. That is, hour in a day (0-23).</li><li class="li">kk denotes 24-hour time format start count of one. That is, hour in a day (1-24).</li><li class="li">hh denotes 12-hour time format in am/pm with a start time of one (1-12).</li><li class="li">KK denotes 12-hour time format in am/pm with a start time of zero (0-11).</li></ul> </li></ul> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1551 ">hh:mm:ss: a<p class="p">HH:mm:ss</p> </td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1545 ">HH:mm:ss z</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1548 "><ul class="ul" id="date-and-time-format-guidelines__ul_clz_g3k_fzb"><li class="li">Problem: Single 'z' time zone display values aren’t unique. For example, IST: India/Israel Standard Time.</li><li class="li">Solution: Use 'zzzz' for the full time zone name.</li></ul> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1551 ">HH:mm:ss zzzz</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1545 ">MM-dd-yyyy HH:MM</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1548 "><ul class="ul" id="date-and-time-format-guidelines__ul_ycl_h3k_fzb"><li class="li">Problem: Using 'MM' for both month and minute.</li><li class="li">Solution: Use 'MM' for the month and 'mm' for the minute.</li></ul> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1551 ">MM-dd-yyyy HH:mm</td></tr><tr class="row"><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1545 ">yyyy-DDD-MM</td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1548 "><ul class="ul" id="date-and-time-format-guidelines__ul_e2q_h3k_fzb"><li class="li">Problem: Using 'DDD' for the day of the month. The 'D' is symbol is used for the day number of the year.</li><li class="li">Solution: Use 'dd' for day of the month.</li></ul> </td><td class="entry cellrowborder" style="vertical-align:top;" headers="d224339e1551 ">yyyy-MM-dd</td></tr></tbody></table> </div> <p class="p">For additional information, refer to the Date and Time Patterns section for <a class="xref" href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html" target="_blank" rel="noopener noreferrer">SimpleDateFormat</a>.</p> </div> </div> </div> </div> </div> </body></html></div>