Difference between revisions of "Counting Business Days between Two Dates"

From SmartWiki
Jump to: navigation, search
 
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
To count business days between two dates, the following code can be  used in a [[Custom Field Type (103): Special – Browser Script|Special Browser script]] field that will populate a [[Custom Field|custom field]] with the number of business days between two given dates.
+
To count business days between two dates (''startdate'' and ''enddate''), the following code can be  used in a [[Custom Field Type: Special – Browser Script|Special Browser script]] field that will populate a [[Custom Field Type: Special – Calculated Value|Calculated Value Custom Field]] with the number of business days between two given dates.
  
 +
In the [[Custom Field Type: Special – Browser Script|Special Browser script]] field you define the function that calculates the difference:
 
<pre>
 
<pre>
  
function busdatediff()
+
function bus_date_diff()
 
{
 
{
var frm=document.form1;
+
  var frm=document.forms[0];
var formatstr="dd/mm/yyyy";
+
  var formatdate="@dateformat@";
var start=ConvertDateStr(frm.startdate.value,formatstr);
+
  var start=ConvertDateStr(frm.startdate.value,formatdate);
var end=ConvertDateStr(frm.enddate.value,formatstr);
+
  var end=ConvertDateStr(frm.enddate.value,formatdate);
var diff=datediff(start,end, "D");
+
  var diff=datediff(start,end, "D");
var i=0;
+
  var i=0;
var busday=0;
+
  var busdays=0;
for(i=0;i<diff;i++)
+
  for(i=0;i<diff;i++)
{
+
  {
  var cdate=dateadd(start,'D',i);
+
    var cdate=dateadd(start,'D',i);
  if(!isHoliday(cdate))
+
    if(!isHoliday(cdate))
    busday++;
+
    busdays++;
}
+
  }
frm.cf_56262.value=busday;
+
  return busdays;
 
}
 
}
busdatediff();
 
  
 
</pre>
 
</pre>
  
[[Category:Custom Fields]][[Category:Applicant Tracking]][[Category:Enhancements]]
+
In '''Expression''' section of the [[Custom Field Type: Special – Calculated Value|Calculated Value Custom Field]] you would call the function:
 +
bus_date_diff()
 +
 
 +
Note: The function definition can be changed to accept parameters (fields containing dates) that are passed at the time of calling the function from any [[Custom Field Type: Special – Calculated Value|Calculated Value Custom Field]].
 +
 
 +
 
 +
 
 +
==See Also==
 +
* [[Template_/_Type_Formula#Counting_and_Adding_Business_Days|Using Template Formulas to Count and Add Business Days]]
 +
* [[Adding Business Days]]
 +
 
 +
[[Category:Custom Fields]][[Category:JavaScript]][[Category: Date Formats]]

Latest revision as of 11:56, 25 September 2013

To count business days between two dates (startdate and enddate), the following code can be used in a Special Browser script field that will populate a Calculated Value Custom Field with the number of business days between two given dates.

In the Special Browser script field you define the function that calculates the difference:


function bus_date_diff()
{
  var frm=document.forms[0];
  var formatdate="@dateformat@";
  var start=ConvertDateStr(frm.startdate.value,formatdate);
  var end=ConvertDateStr(frm.enddate.value,formatdate);
  var diff=datediff(start,end, "D");
  var i=0;
  var busdays=0;
  for(i=0;i<diff;i++)
  {
    var cdate=dateadd(start,'D',i);
    if(!isHoliday(cdate))
    busdays++;
  }
  return busdays;
}

In Expression section of the Calculated Value Custom Field you would call the function:

bus_date_diff()

Note: The function definition can be changed to accept parameters (fields containing dates) that are passed at the time of calling the function from any Calculated Value Custom Field.


See Also