<?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: Calculate days left in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569022#M225948</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Unfortunately, for the duration field type, if you want only days to show, it can't be limited to just that. The attribute normally used would be max_time= and then days, hours, etc. While you can use "days"...that's the max unit used, meaning the lower unit of measurements such as hours and seconds, are still valid and would show. If you wanted only hours and no days, then this is where it's beneficial such as max_time=hours. Then...days would NOT show.&lt;/P&gt;
&lt;P&gt;It sounds like you may need to use a string field or some other field type, do the calculation, format it as needed, then set the value for this field.&lt;/P&gt;
&lt;P&gt;Even if you could display only days, you still wanted additional customization around it showing Expired, and it wouldn't be able to do that either and further justifying the need for a different field type.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Please mark reply as Helpful/Correct, if applicable. Thanks!&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Jun 2022 16:45:05 GMT</pubDate>
    <dc:creator>Allen Andreas</dc:creator>
    <dc:date>2022-06-13T16:45:05Z</dc:date>
    <item>
      <title>Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569021#M225947</link>
      <description>&lt;P&gt;Hello - Looking to calculate a Contract Days Left field in the contract application form.&lt;/P&gt;
&lt;P&gt;What I have works for the most part, but I would like the output to show 0 or "Expired".&lt;/P&gt;
&lt;P&gt;Instead it shows hours and minutes that simply do not apply.&lt;/P&gt;
&lt;P&gt;Also, Any to only show days and NOT show hours and minutes?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/190884i900B0BEE027DFA77/image-size/large?v=v2&amp;amp;px=999" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/190885iF708162780A810DC/image-size/large?v=v2&amp;amp;px=999" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 15:55:02 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569021#M225947</guid>
      <dc:creator>Chris Richie</dc:creator>
      <dc:date>2022-06-13T15:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569022#M225948</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Unfortunately, for the duration field type, if you want only days to show, it can't be limited to just that. The attribute normally used would be max_time= and then days, hours, etc. While you can use "days"...that's the max unit used, meaning the lower unit of measurements such as hours and seconds, are still valid and would show. If you wanted only hours and no days, then this is where it's beneficial such as max_time=hours. Then...days would NOT show.&lt;/P&gt;
&lt;P&gt;It sounds like you may need to use a string field or some other field type, do the calculation, format it as needed, then set the value for this field.&lt;/P&gt;
&lt;P&gt;Even if you could display only days, you still wanted additional customization around it showing Expired, and it wouldn't be able to do that either and further justifying the need for a different field type.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Please mark reply as Helpful/Correct, if applicable. Thanks!&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 16:45:05 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569022#M225948</guid>
      <dc:creator>Allen Andreas</dc:creator>
      <dc:date>2022-06-13T16:45:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569023#M225949</link>
      <description>&lt;P&gt;Understood.&amp;nbsp; So If I create a String Field, what would be the code to calculate?&amp;nbsp; Also, Would I need to do it via Business rule?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 17:09:35 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569023#M225949</guid>
      <dc:creator>Chris Richie</dc:creator>
      <dc:date>2022-06-13T17:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569024#M225950</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Yes, setting the value with a Business rule would be recommended.&lt;/P&gt;
&lt;P&gt;A before insert/update business rule, that runs whenever End date changes AND End date is not empty.&lt;/P&gt;
&lt;P&gt;In the script you could run something like this:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;(function executeRule(current, previous /*null when async*/) {

	var now  = new GlideDateTime();
	var nowDate = now.getDate();
	now.setValue(nowDate + ' 00:00:00'); // force the time to be able to compare with a date field.
	var endDate = current.getValue('ends');
	var endDateTime = new GlideDateTime(endDate + ' 00:00:00');
	var daysDuration = GlideDateTime.subtract(now, endDateTime);
	current.setValue('u_days_until_expire2', daysDuration.getDayPart());

})(current, previous);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 17:38:45 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569024#M225950</guid>
      <dc:creator>OlaN</dc:creator>
      <dc:date>2022-06-13T17:38:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569025#M225951</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I hope my initial reply was at least &lt;STRONG&gt;helpful&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;Ideally, you'd attempt this on your end and we'd have a dialog to help you learn instead of just having code be given to you. Since that has already happened by someone else replying in my reply chain here, then feel free to work with that &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;A business rule would be ideal to do this so that it doesn't involve the client however, depending on if that result determines actions the client needs to do in that moment, then you can use an onChange client script, for example, and GlideAjax.&lt;/P&gt;
&lt;P&gt;We don't know the full use case here, so that's for you to decide.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;Please mark reply as Helpful/Correct, if applicable. Thanks!&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 23:22:20 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569025#M225951</guid>
      <dc:creator>Allen Andreas</dc:creator>
      <dc:date>2022-06-13T23:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569026#M225952</link>
      <description>&lt;P&gt;Sorry for jumping ahead &lt;SN-MENTION class="sn-mention" table="live_profile" sysid="169ecae9dbd41fc09c9ffb651f961975"&gt;@Allen Andreas&lt;/SN-MENTION&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Chris, just like Allen mentioned, there are a couple of options to choose from, depending on your requirement.&lt;/P&gt;
&lt;P&gt;My solution will set the value when the save happens, so in that case the end user will not see the resulting days left until after the Save/update button is pressed. And perhaps you need to display the result directly whenever the value changes, then you would go for a different approach with GlideAjax/Script include.&lt;/P&gt;
&lt;P&gt;Also, in my example the value calculated/stored is not dynamic, so if you set an end date that is 7 days ahead, and then look at the same record 2 days later, it will still say that it's 7 days until expiration, which might be a bit misleading.&lt;/P&gt;
&lt;P&gt;So another option would be to calculate the value whenever the record is loaded/displayed to the user.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 06:16:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569026#M225952</guid>
      <dc:creator>OlaN</dc:creator>
      <dc:date>2022-06-14T06:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569027#M225953</link>
      <description>&lt;P&gt;Thanks for the help.&lt;/P&gt;
&lt;P&gt;My requirement is more when the form is loaded to calculate the days left in the contract.&amp;nbsp; I also will mimic this behavior for another field.&amp;nbsp; It sounds like a GlideAjax/Script would be needed, but have no idea where to start there.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A dynamic value calculated would be ideal as when form is loaded it calculates the "Contract End Date" minus "current date" = &lt;STRONG&gt;"Days Left"&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also appreciate insight to figure this out on my own, but I'm very inexperienced to code, and just want to get this particular requirement satisfied for the business.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 11:54:25 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569027#M225953</guid>
      <dc:creator>Chris Richie</dc:creator>
      <dc:date>2022-06-14T11:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569028#M225954</link>
      <description>&lt;P&gt;Then, by changing the business rule I proposed to instead run on "display" and change the condition to only run if the end date is not empty should do the trick.&lt;/P&gt;
&lt;P&gt;Another small note, I sure hope that there isn't any requirements in regards to reporting on this field. For example how many contracts do we have that has an expiry days less than 30 days.&lt;/P&gt;
&lt;P&gt;That would still lead to the same issue as explained before, the values are not saved on the record, the value is only calculated when the record is displayed. If reporting is needed then reporting by using data on the "End date" field would be required.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jun 2022 20:31:22 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/1569028#M225954</guid>
      <dc:creator>OlaN</dc:creator>
      <dc:date>2022-06-14T20:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate days left</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/2420692#M943566</link>
      <description>&lt;P&gt;How do we get the value to display in the list view? It current works in the form view&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="latroyamitchell_0-1671489789852.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/226941iAA8BED6CD7FF3329/image-size/medium?v=v2&amp;amp;px=400" alt="latroyamitchell_0-1671489789852.png" title="latroyamitchell_0-1671489789852.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Dec 2022 22:43:15 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-days-left/m-p/2420692#M943566</guid>
      <dc:creator>latroyamitchell</dc:creator>
      <dc:date>2022-12-19T22:43:15Z</dc:date>
    </item>
  </channel>
</rss>

