Difference between revisions of "IsHoliday"

From SmartWiki
Jump to: navigation, search
 
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
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.
+
To subtract business days from a given date, 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 new date.
  
In the [[Custom Field Type: Special – Browser Script|Special Browser script]] field you define the function that calculates the difference:
+
In the [[Custom Field Type: Special – Browser Script|Special Browser script]] field you define the function that subtracts the a number of days from a given date
 
<pre>
 
<pre>
  
function bus_date_diff()
+
function bus_date_subtract()
 
{
 
{
   var frm=document.forms[0];
+
   var frm=document.form1; // or document.frmevent at Level 2 or 3
   var formatdate="@dateformat@";
+
   var formatstr="dd/mm/yyyy";
   var start=ConvertDateStr(frm.startdate.value,formatdate);
+
   var start=ConvertDateStr(frm.startdate.value,formatstr);
  var end=ConvertDateStr(frm.enddate.value,formatdate);
+
   var sub=-5;
  var diff=datediff(start,end, "D");
+
   for(i=-1;i>sub;i--)
   var i=0;
 
  var busdays=0;
 
   for(i=0;i<diff;i++)
 
 
   {
 
   {
 
     var cdate=dateadd(start,'D',i);
 
     var cdate=dateadd(start,'D',i);
     if(!isHoliday(cdate))
+
    var cdatetemp=new Date();
    busdays++;
+
    var cdatearr=cdate.split("-");
 +
    cdatetemp.setFullYear(parseInt(cdatearr[0]),parseInt(cdatearr[1])-1,parseInt(cdatearr[2]));
 +
     if(isHoliday(cdate)||cdatetemp.getDate()==0||cdatetemp.getDate()==6)sub--;
 
   }
 
   }
  return busdays;
+
document.frmevent.cf_1742956.value=cdate;
 
}
 
}
 +
bus_date_subtract();
  
 
</pre>
 
</pre>
  
 
In '''Expression''' section of the [[Custom Field Type: Special – Calculated Value|Calculated Value Custom Field]] you would call the function:
 
In '''Expression''' section of the [[Custom Field Type: Special – Calculated Value|Calculated Value Custom Field]] you would call the function:
  bus_date_diff()
+
  bus_date_subtract();
  
 
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]].  
 
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]].  
[[Category:Custom Fields]][[Category:JavaScript]]
+
 
 +
==See Also==
 +
* [[Manipulating Dates]]
 +
* [[busdateadd]]
 +
* [[Custom Defined Holidays]]
 +
 
 +
[[Category:Custom Fields]][[Category:JavaScript]][[Category:Date Formats]]

Latest revision as of 11:08, 26 August 2013

To subtract business days from a given date, the following code can be used in a Special Browser script field that will populate a Calculated Value Custom Field with the new date.

In the Special Browser script field you define the function that subtracts the a number of days from a given date


function bus_date_subtract()
{
  var frm=document.form1;  // or document.frmevent at Level 2 or 3
  var formatstr="dd/mm/yyyy";
  var start=ConvertDateStr(frm.startdate.value,formatstr);
  var sub=-5;
  for(i=-1;i>sub;i--)
  {
    var cdate=dateadd(start,'D',i);
    var cdatetemp=new Date();
    var cdatearr=cdate.split("-");
    cdatetemp.setFullYear(parseInt(cdatearr[0]),parseInt(cdatearr[1])-1,parseInt(cdatearr[2]));
    if(isHoliday(cdate)||cdatetemp.getDate()==0||cdatetemp.getDate()==6)sub--;
  }
document.frmevent.cf_1742956.value=cdate;
}
bus_date_subtract();

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

bus_date_subtract();

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