nerdygirl
03-19-2003, 08:45 AM
I have a script that calculates the difference between 2 dates and works out the amount of working days but I am having real problems handling bank holidays.
Here is my script so far. Does anyone have any ideas how to handle the hols?
<script language="javascript">
function getDifference() {
// grab values from fields
var datefrom = new Date(document.FrontPage_Form1.datefromunformat.value);
var dateto = new Date(document.FrontPage_Form1.datetounformat.value);
//check to see which radio button is selected
var daytypefrom
if (document.FrontPage_Form1.typefrom[0].checked==true)
daytypefrom ="Day";
document.FrontPage_Form1.typefrom[1].checked==false
document.FrontPage_Form1.typefrom[2].checked==false
if (document.FrontPage_Form1.typefrom[1].checked==true)
daytypefrom = "AM";
document.FrontPage_Form1.typefrom[0].checked==false
document.FrontPage_Form1.typefrom[2].checked==false
if (document.FrontPage_Form1.typefrom[2].checked==true)
daytypefrom = "PM";
document.FrontPage_Form1.typefrom[0].checked==false
document.FrontPage_Form1.typefrom[1].checked==false
// check to see which radio button on to set has been selected
var daytype;
if (document.FrontPage_Form1.type[0].checked==true)
daytype ="Day";
document.FrontPage_Form1.type[1].checked==false
document.FrontPage_Form1.type[2].checked==false
if (document.FrontPage_Form1.type[1].checked==true)
daytype = "AM";
document.FrontPage_Form1.type[0].checked==false
document.FrontPage_Form1.type[2].checked==false
if (document.FrontPage_Form1.type[2].checked==true)
daytype = "PM";
document.FrontPage_Form1.type[0].checked==false
document.FrontPage_Form1.type[1].checked==false
//get date in milliseconds
var datefromtime=datefrom.getTime();
var datetotime=dateto.getTime();
var tempdate = new Date(datefrom);
var daycount=0;
// if not sat or sunday then add 1 to include today
if ( (tempdate.getDay(tempdate)) % 6 !=0 )
daycount = daycount +1;
//if a half day has been selected then subtract 0.5 at the beginning
if (daytypefrom =="AM" || daytypefrom =="PM")
daycount = daycount - 0.5;
//alert(daycount);
// loop to get date in milliseconds and add a day in milliseconds to it
while (tempdate < datetotime)
{
tempdate.setTime(tempdate.getTime() + (1000*60*60*24));
var tempday = tempdate.getDay(tempdate);
if (tempday % 6 !=0 )
// alert("sunday");
daycount = daycount +1;
}
//if the date to is a half day then subtract 0.5
if (daytype =="AM" || daytype =="PM")
daycount = daycount - 0.5;
//write out the value to the page
document.FrontPage_Form1.daysrequired.value=daycount;
}
</script>
Here is my script so far. Does anyone have any ideas how to handle the hols?
<script language="javascript">
function getDifference() {
// grab values from fields
var datefrom = new Date(document.FrontPage_Form1.datefromunformat.value);
var dateto = new Date(document.FrontPage_Form1.datetounformat.value);
//check to see which radio button is selected
var daytypefrom
if (document.FrontPage_Form1.typefrom[0].checked==true)
daytypefrom ="Day";
document.FrontPage_Form1.typefrom[1].checked==false
document.FrontPage_Form1.typefrom[2].checked==false
if (document.FrontPage_Form1.typefrom[1].checked==true)
daytypefrom = "AM";
document.FrontPage_Form1.typefrom[0].checked==false
document.FrontPage_Form1.typefrom[2].checked==false
if (document.FrontPage_Form1.typefrom[2].checked==true)
daytypefrom = "PM";
document.FrontPage_Form1.typefrom[0].checked==false
document.FrontPage_Form1.typefrom[1].checked==false
// check to see which radio button on to set has been selected
var daytype;
if (document.FrontPage_Form1.type[0].checked==true)
daytype ="Day";
document.FrontPage_Form1.type[1].checked==false
document.FrontPage_Form1.type[2].checked==false
if (document.FrontPage_Form1.type[1].checked==true)
daytype = "AM";
document.FrontPage_Form1.type[0].checked==false
document.FrontPage_Form1.type[2].checked==false
if (document.FrontPage_Form1.type[2].checked==true)
daytype = "PM";
document.FrontPage_Form1.type[0].checked==false
document.FrontPage_Form1.type[1].checked==false
//get date in milliseconds
var datefromtime=datefrom.getTime();
var datetotime=dateto.getTime();
var tempdate = new Date(datefrom);
var daycount=0;
// if not sat or sunday then add 1 to include today
if ( (tempdate.getDay(tempdate)) % 6 !=0 )
daycount = daycount +1;
//if a half day has been selected then subtract 0.5 at the beginning
if (daytypefrom =="AM" || daytypefrom =="PM")
daycount = daycount - 0.5;
//alert(daycount);
// loop to get date in milliseconds and add a day in milliseconds to it
while (tempdate < datetotime)
{
tempdate.setTime(tempdate.getTime() + (1000*60*60*24));
var tempday = tempdate.getDay(tempdate);
if (tempday % 6 !=0 )
// alert("sunday");
daycount = daycount +1;
}
//if the date to is a half day then subtract 0.5
if (daytype =="AM" || daytype =="PM")
daycount = daycount - 0.5;
//write out the value to the page
document.FrontPage_Form1.daysrequired.value=daycount;
}
</script>