Difference between revisions of "Counting Business Days between Two Dates"
From SmartWiki
Julia Decker (talk | contribs) |
|||
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| | + | 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 Type (60): Special – Calculated Value|Calculated Value Custom Field]] with the number of business days between two given dates. |
<pre> | <pre> | ||
− | function | + | function bus_date_diff() |
{ | { | ||
− | var frm=document. | + | var frm=document.forms[0]; |
− | var | + | var formatdate="@dateformat@"; |
− | var start=ConvertDateStr(frm.startdate.value, | + | var start=ConvertDateStr(frm.startdate.value,formatdate); |
− | var end=ConvertDateStr(frm.enddate.value, | + | 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 | + | var busdays=0; |
− | for(i=0;i<diff;i++) | + | for(i=0;i<diff;i++) |
− | { | + | { |
− | + | var cdate=dateadd(start,'D',i); | |
− | + | if(!isHoliday(cdate)) | |
− | + | busdays++; | |
− | } | + | } |
− | + | return busdays; | |
} | } | ||
− | |||
</pre> | </pre> | ||
− | [[ | + | In the '''Expression''' section of the [[Custom Field Type (60): Special – Calculated Value|Calculated Value Custom Field]] you would put the following: |
+ | bus_date_diff() | ||
+ | |||
+ | |||
+ | [[Category:Custom Fields]][[Category:JavaScript Examples]] |
Revision as of 09:41, 7 April 2009
To count business days between two dates, 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.
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 the Expression section of the Calculated Value Custom Field you would put the following:
bus_date_diff()