PDA

View Full Version : Workdays Elapsed


Ancora
11-28-2005, 06:02 PM
<html>
<head>
<script type="text/javascript">

// 2005 & 2006 U.S. federal holidays. January 20th, 2005 was Inauguration Day.
var skipDate = new Array('1/17/2005','1/20/2005','2/21/2005','5/30/2005','7/4/2005','9/5/2005','10/10/2005','11/11/2005','11/24/2005','1/16/2006','2/20/2006','5/29/2006','7/4/2006','9/4/2006','10/9/2006','11/23/2006','12/25/2006')

function verify(isField){

var splitDate = isField.value.split("/");
var refDate = new Date(isField.value);
if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))){return false}
return refDate;
}

function holidays(dStart,dEnd){

var t = 0;
for (i=0; i<skipDate.length; i++)
{
var tmp = skipDate[i].split("/")
var refDate = new Date(tmp[0]+"/"+tmp[1]+"/"+tmp[2]);
if (refDate >= dStart && refDate <= dEnd){t++}
}
return t;
}

function countDays(isForm){

var n =0;
var startDate = verify(isForm.nStart);
if (startDate){var endDate = verify(isForm.nEnd)}
if (startDate && endDate && (endDate < startDate))
{
var tmp = startDate;
for (i=0; tmp > endDate; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()-i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n--}
}
if (startDate.getDay() != 6 && startDate.getDay() != 0){n++}
isForm.nWeekdays.value = n+holidays(endDate,startDate);
}
if (startDate && endDate && (endDate > startDate))
{
var tmp = startDate;
for (i=0; tmp < endDate; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n++}
}
if (tmp.getDay() != 6 && tmp.getDay() != 0){n--}
isForm.nWeekdays.value = n-holidays(startDate,endDate);
}
if (!startDate)
{
alert('Invalid Start Date')
isForm.nStart.value = "";
isForm.nStart.focus();
}
else if (!endDate)
{
alert('Invalid End Date')
isForm.nEnd.value = "";
isForm.nEnd.focus();
}
}

</script>
</head>
<body>
<br>
<form name='Form1'>
<Table align='center' cellspacing='0' cellpadding='5' style='font-size:14pt;border:solid black 1px;background-color:lightyellow'>
<THead>
<TH colspan='2' style='background-color:lightblue;border-bottom:solid black 1px'>Workdays Elapsed</TH>
</THead>
<TFoot>
<TR><TD colspan='2' align='center' style='border-top:solid black 1px;Font-Size:11pt;background-color:moccasin'>Input format = mm/dd/yyyy<br>An ending workday hasn't elapsed</TD></TR>
</TFoot>
<TBody>
<TR>
<TD align='left'>Start Date: </TD><TD align='right'><input type='text' size='9' name='nStart' onclick="this.value='';this.form.nWeekdays.value=''"></TD>
</TR>
<TR>
<TD align='left'>End Date: </TD><TD align='right'><input type='text' size='9' name='nEnd' onclick="this.value='';this.form.nWeekdays.value=''"></TD>
</TR>
<TR>
<TD align='left'>Workdays: </TD><TD align='right'><input type='text' size='9' readonly name='nWeekdays' style='text-align:right'></TD>
</TR>
<TR>
<TD colspan='2' align='center' style='border-top:solid black 1px;background-color:darkorange'><input type='button' value='Calculate' onclick="countDays(this.form)"></TD>
</TR>
</TBody>
</Table>
</form>
</body>
</html>

midaco
02-03-2007, 11:12 AM
hi ,

im using your script and i have some problem .
like : in January 2007 i have 31 days - 2 holidays - 4 sun. = 25 working days .
and the script give me 22 days
<html>
<head>
<script type="text/javascript">

// 2005 & 2006 U.S. federal holidays. January 20th, 2005 was Inauguration Day.
var skipDate = new Array('01/01/2007','01/06/2007','04/09/2007','04/25/2007','05/01/2007','08/15/2007','11/01/2007','12/08/2007','12/25/2007','12/26/2007')

function verify(isField){

var splitDate = isField.value.split("/");
var refDate = new Date(isField.value);
if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))){return false}
return refDate;
}

function holidays(dStart,dEnd){

var t = 0;
for (i=0; i<skipDate.length; i++)
{
var tmp = skipDate[i].split("/")
var refDate = new Date(tmp[0]+"/"+tmp[1]+"/"+tmp[2]);
if (refDate >= dStart && refDate <= dEnd){t++}
}
return t;
}

function countDays(isForm){

var n =0;
var startDate = verify(isForm.nStart);
if (startDate){var endDate = verify(isForm.nEnd)}
if (startDate && endDate && (endDate < startDate))
{
var tmp = startDate;
for (i=0; tmp > endDate; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()-i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n--}
}
if (startDate.getDay() != 6 && startDate.getDay() != 0){n++}
isForm.nWeekdays.value = n+holidays(endDate,startDate);
}
if (startDate && endDate && (endDate > startDate))
{
var tmp = startDate;
for (i=0; tmp < endDate; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n++}
}
if (tmp.getDay() != 6 && tmp.getDay() != 0){n--}
isForm.nWeekdays.value = n-holidays(startDate,endDate);
}
if (!startDate)
{
alert('Invalid Start Date')
isForm.nStart.value = "";
isForm.nStart.focus();
}
else if (!endDate)
{
alert('Invalid End Date')
isForm.nEnd.value = "";
isForm.nEnd.focus();
}
}

</script>
</head>
<body>
<br>
<form name='Form1'>
<Table align='center' cellspacing='0' cellpadding='5' style='font-size:14pt;border:solid black 1px;background-color:lightyellow'>
<THead>
<TH colspan='2' style='background-color:lightblue;border-bottom:solid black 1px'>Workdays Elapsed</TH>
</THead>
<TFoot>
<TR><TD colspan='2' align='center' style='border-top:solid black 1px;Font-Size:11pt;background-color:moccasin'>Input format = mm/dd/yyyy<br>An ending workday hasn't elapsed</TD></TR>
</TFoot>
<TBody>
<TR>
<TD align='left'>Start Date: </TD><TD align='right'><input type='text' size='9' name='nStart' onclick="this.value='';this.form.nWeekdays.value=''"></TD>
</TR>
<TR>
<TD align='left'>End Date: </TD><TD align='right'><input type='text' size='9' name='nEnd' onclick="this.value='';this.form.nWeekdays.value=''"></TD>
</TR>
<TR>
<TD align='left'>Workdays: </TD><TD align='right'><input type='text' size='9' readonly name='nWeekdays' style='text-align:right'></TD>
</TR>
<TR>
<TD colspan='2' align='center' style='border-top:solid black 1px;background-color:darkorange'><input type='button' value='Calculate' onclick="countDays(this.form)"></TD>
</TR>
</TBody>
</Table>
</form>
</body>
</html>

were is the problem ???