<?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 the difference between start date and end date and populate the difference between them in another field in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575905#M232831</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Please refer below link, it might helpful to you.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.servicenow.com/community?id=community_question&amp;amp;sys_id=a67503eddbd8dbc01dcaf3231f961958"&gt;https://community.servicenow.com/community?id=community_question&amp;amp;sys_id=a67503eddbd8dbc01dcaf3231f961958&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Mark it correct and helpful.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Bhagyashri Sorte.&lt;/P&gt;</description>
    <pubDate>Mon, 07 Sep 2020 07:42:17 GMT</pubDate>
    <dc:creator>Bhagyashri Sort</dc:creator>
    <dc:date>2020-09-07T07:42:17Z</dc:date>
    <item>
      <title>Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575902#M232828</link>
      <description>&lt;P&gt;I have two fields start_date and end_date.I wanted to writ a client script so that when i select both dates the difference should be populated in another field called no_of_days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Sep 2020 07:27:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575902#M232828</guid>
      <dc:creator>test1231998</dc:creator>
      <dc:date>2020-09-07T07:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575903#M232829</link>
      <description>&lt;P&gt;Make sure no_of_days field is of type duration and use below script in a Before Insert Business rule:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;getTimeDiff();

function getTimeDiff() {
    var startDate = current.u_start_date.getGlideObject();
    var endDate = current.u_end_date.getGlideObject();

    current.no_of_days = gs.dateDiff(startDate.getDisplayValueInternal(), endDate.getDisplayValueInternal(), false);


}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;reference:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.servicenow.com/community?id=community_question&amp;amp;sys_id=fa3dc7a9db9cdbc01dcaf3231f96196b" rel="nofollow"&gt;https://community.servicenow.com/community?id=community_question&amp;amp;sys_id=fa3dc7a9db9cdbc01dcaf3231f96196b&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Sep 2020 07:33:06 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575903#M232829</guid>
      <dc:creator>Willem</dc:creator>
      <dc:date>2020-09-07T07:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575904#M232830</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Please refer to this link where i have provided sample code on how to do date validations using client script.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.servicenow.com/community?id=community_article&amp;amp;sys_id=a26dac761b4e8010a59033f2cd4bcb12" target="_blank"&gt;https://community.servicenow.com/community?id=community_article&amp;amp;sys_id=a26dac761b4e8010a59033f2cd4bcb12&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Mark the comment as a correct answer and helpful if this helps.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Sep 2020 07:38:08 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575904#M232830</guid>
      <dc:creator>asifnoor</dc:creator>
      <dc:date>2020-09-07T07:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575905#M232831</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Please refer below link, it might helpful to you.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.servicenow.com/community?id=community_question&amp;amp;sys_id=a67503eddbd8dbc01dcaf3231f961958"&gt;https://community.servicenow.com/community?id=community_question&amp;amp;sys_id=a67503eddbd8dbc01dcaf3231f961958&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Mark it correct and helpful.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Bhagyashri Sorte.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Sep 2020 07:42:17 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575905#M232831</guid>
      <dc:creator>Bhagyashri Sort</dc:creator>
      <dc:date>2020-09-07T07:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575906#M232832</link>
      <description>&lt;P&gt;Hi Deepika,&lt;/P&gt;
&lt;P&gt;you will have to write onchange client script on both start_date and end_date fields&lt;/P&gt;
&lt;P&gt;Sample script below&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Script Include:&lt;/STRONG&gt; It should be client callable&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var DateCalculations = Class.create();
DateCalculations.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getDifference: function(){

		var startDate = new GlideDateTime(this.getParameter('sysparm_startDate'));	
        var endDate = new GlideDateTime(this.getParameter('sysparm_endDate'));		
		var diffSeconds = gs.dateDiff(startDate.getDisplayValue(), endDate.getDisplayValue(), true);
		var days = parseInt(diffSeconds)*60*24;
        return  days; 		
	},
	
    type: 'DateCalculations'
});&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Client Script:&lt;/STRONG&gt; onChange of Start; similarly you need to have onChange of End&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }

if(newValue == ''){
	g_form.clearValue('no_of_days');
}

if(oldValue != newValue){

var ga = new GlideAjax('DateCalculations');
    ga.addParam('sysparm_name', "getDifference");
    ga.addParam('sysparm_startDate', g_form.getValue('start_date'));
	ga.addParam('sysparm_endDate', g_form.getValue('end_date'));
    ga.getXMLAnswer(function(answer){
        if(answer != ''){
            g_form.setValue('no_of_days', answer); // give proper field name here
        }
    });
    //Type appropriate comment here, and begin script below
}
}&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Regards&lt;BR /&gt;Ankur&lt;/P&gt;</description>
      <pubDate>Mon, 07 Sep 2020 07:57:09 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575906#M232832</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2020-09-07T07:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575907#M232833</link>
      <description>&lt;P&gt;Thanks Ankur,this worked&lt;/P&gt;</description>
      <pubDate>Mon, 07 Sep 2020 15:17:14 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575907#M232833</guid>
      <dc:creator>test1231998</dc:creator>
      <dc:date>2020-09-07T15:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575908#M232834</link>
      <description>&lt;P&gt;Hi Deepika,&lt;/P&gt;
&lt;P&gt;I thought you wanted to do validation using client scripts. Did you check the article that i have provided above?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 18:04:04 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575908#M232834</guid>
      <dc:creator>asifnoor</dc:creator>
      <dc:date>2020-09-08T18:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate the difference between start date and end date and populate the difference between them in another field</title>
      <link>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575909#M232835</link>
      <description>Hello sir, 
In onChange client script
Sir what type field have you taken?</description>
      <pubDate>Sat, 05 Mar 2022 01:08:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/calculate-the-difference-between-start-date-and-end-date-and/m-p/1575909#M232835</guid>
      <dc:creator>Arvind Kumar1</dc:creator>
      <dc:date>2022-03-05T01:08:31Z</dc:date>
    </item>
  </channel>
</rss>

