<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Date format subtraction issue with GlideDateTime in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766526#M423452</link>
    <description>&lt;P&gt;Thank you Janos. This helps explain things for me and will be useful to come back to in the future!&lt;/P&gt;</description>
    <pubDate>Wed, 03 Nov 2021 12:11:45 GMT</pubDate>
    <dc:creator>mattystern</dc:creator>
    <dc:date>2021-11-03T12:11:45Z</dc:date>
    <item>
      <title>Date format subtraction issue with GlideDateTime</title>
      <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766522#M423448</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a field within a scoped application for my customer service team which calculates "Resolution Days." I am currently using a business rule to calculate this by getting the "Inquiry Date" and "Resolution Date" fields and subtracting the two, then converting this to days from milliseconds. This was working fine until I added my European user base. It seems the subtraction is having trouble subtracting dates in dd-MM-yyyy format and coming up with incorrect results, as the system default is MM-dd-yyyy.&lt;/P&gt;
&lt;P&gt;I tried to change this by setting the GlideDateTime's format with setDisplayValue. However, this is throwing an&amp;nbsp;error:&lt;BR /&gt;&lt;EM&gt;exception: org.mozilla.javascript.EvaluatorException: Can't find method com.glide.glideobject.GlideDateTime.subtract(undefined,com.glide.glideobject.GlideDateTime).&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Below is my business rule:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;(function executeRule(current, previous /*null when async*/) {
	//store inquiry and resolution dates
	var inquiryDate = current.getDisplayValue('u_inquiry_date');
	var resolutionDate = current.getDisplayValue('u_resolution_date');


	if(inquiryDate != ''){
		//starts new GlideDateTime object using inquiryDate
		var startDate = new GlideDateTime (inquiryDate);
		//line which is not working 
	//	startDate = startDate.setDisplayValue(inquiryDate, "MM-dd-yyyy HH:mm:ss");
		var endDate = new GlideDateTime(gs.nowDateTime());
		if(current.getValue('u_state') != 'Open' &amp;amp;&amp;amp; resolutionDate != ''){
			endDate = new GlideDateTime(resolutionDate);
	//		endDate = endDate.setDisplayValue(resolutionDate, "MM-dd-yyyy HH:mm:ss");
		}
		var dur = GlideDateTime.subtract(startDate, endDate);
		//convert the answer from milliseconds to days
		var days = dur.getNumericValue() /  (1000*60*60*24);
		if(days &amp;lt; 1){
			days = 1;
		}
		current.u_resolution_time = days;
	}
	else{
		current.u_resolution_time = 0;
	}

})(current, previous);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I likely think this is just me not understanding the syntax of using the setDisplayValue() method and how it works. I have read the&amp;nbsp;&lt;A href="https://developer.servicenow.com/dev.do#!/reference/api/quebec/server/no-namespace/c_APIRef"&gt;developer documentation on setDisplayValue&lt;/A&gt;, but it is not sinking in for me.&amp;nbsp;Can anyone offer assistance on when I should be converting the date's format / how this is achieved or just general tips for this? Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 18:52:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766522#M423448</guid>
      <dc:creator>mattystern</dc:creator>
      <dc:date>2021-11-02T18:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: Date format subtraction issue with GlideDateTime</title>
      <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766523#M423449</link>
      <description>&lt;P&gt;You should not be using date display values on server side - it just complicates things while adding nothing to the outcome. If you insist, you should on one hand use &lt;CODE&gt;.getDisplayValueInternal()&lt;/CODE&gt; instead on the other hand use it throughout. It returns the date and time value in the system format.&lt;/P&gt;
&lt;P&gt;All that also means you can't use &lt;CODE&gt;new GlideDateTime (inquiryDate);&lt;/CODE&gt;: it expects&amp;nbsp;&lt;CODE&gt;inquiryDate&lt;/CODE&gt; to be the date and time in the system TZ (UTC by default), but it will most likely not be as the value has been obtained using a display value method - which returns the date and time in the current user's TZ.&lt;/P&gt;
&lt;P&gt;That is why, just don't use the display value methods on server side.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Nov 2021 19:34:10 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766523#M423449</guid>
      <dc:creator>-O-</dc:creator>
      <dc:date>2021-11-02T19:34:10Z</dc:date>
    </item>
    <item>
      <title>Re: Date format subtraction issue with GlideDateTime</title>
      <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766524#M423450</link>
      <description>&lt;P&gt;Try&amp;nbsp;the below.&amp;nbsp; It should be doing everything in GMT.&lt;/P&gt;
&lt;P&gt;Also why do you care if its open before you use the resolution date?&amp;nbsp; Why not just check if it has a value and if it does then use it?&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;(function executeRule(current, previous /*null when async*/) {

	if(current.u_inquiry_date){
		var startDate = current.u_inquiry_date.getGlideObject();
		var endDate = new GlideDateTime();
		if(current.getValue('u_state') != 'Open' &amp;amp;&amp;amp; current.u_resolution_date){
			endDate = current.u_resolution_date.getGlideObject();
		}
		var dur = endDate.getNumericValue() - startDate.getNumericValue();
		//convert the answer from milliseconds to days
		var days = dur /  (1000*60*60*24);
		if(days &amp;lt; 1){
			days = 1;
		}
		current.u_resolution_time = days;
	} else {
		current.u_resolution_time = 0;
	}

})(current, previous);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Nov 2021 19:37:55 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766524#M423450</guid>
      <dc:creator>DrewW</dc:creator>
      <dc:date>2021-11-02T19:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Date format subtraction issue with GlideDateTime</title>
      <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766525#M423451</link>
      <description>&lt;P&gt;Hi Drew,&lt;/P&gt;
&lt;P&gt;This (combined with Janos' response) helped me immensely! Thank you so much!&lt;/P&gt;
&lt;P&gt;-Matt&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 12:08:48 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766525#M423451</guid>
      <dc:creator>mattystern</dc:creator>
      <dc:date>2021-11-03T12:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: Date format subtraction issue with GlideDateTime</title>
      <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766526#M423452</link>
      <description>&lt;P&gt;Thank you Janos. This helps explain things for me and will be useful to come back to in the future!&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 12:11:45 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766526#M423452</guid>
      <dc:creator>mattystern</dc:creator>
      <dc:date>2021-11-03T12:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: Date format subtraction issue with GlideDateTime</title>
      <link>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766527#M423453</link>
      <description>&lt;P&gt;You're most welcome &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 12:24:23 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/date-format-subtraction-issue-with-glidedatetime/m-p/1766527#M423453</guid>
      <dc:creator>-O-</dc:creator>
      <dc:date>2021-11-03T12:24:23Z</dc:date>
    </item>
  </channel>
</rss>

