homerUK
10-17-2005, 03:47 PM
I'm trying to work out the different between two times in decimal.... but its not working...
eg: start time = 10.00
end time = 11.00
the difference should be 1.00, but its coming out as 1.10
starttime = 13.30
end time = 15.00
it should be 1.5 hours difference, yet it comes up with 1.15!
you can see it in action here: http://www.mattfacer.com/test.htm
here's the entire code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function calculateHours() {
//work out the fractional times
message = "";
if (document.frm.endTime.value == "") { endTime = "0.00"; } else { endTime = document.frm.endTime.value; }
if (document.frm.startTime.value == "") { startTime = "0.00"; } else { startTime = document.frm.startTime.value; }
if ((document.frm.endTime.value == "") || (document.frm.endTime.value == "")) {
message = "Could not calculate - Both start and end times must be entered";
}
//times
if (document.frm.startTime.value > document.frm.endTime.value) {
message = "Could not calculate - The start time cannot be greater than the end time";
}
var startArray = startTime.split(".");
var endArray = endTime.split(".");
//minutes
var startminutes = startArray[0]*60 + startArray[1]*1;
var endminutes = endArray[0]*60 + endArray[1]*1;
var totalminutes = endminutes - startminutes;
var fractionMinutes = (totalminutes/60)*100;
var totalhours = Math.floor(totalminutes / 60);
var totalminutes = totalminutes%60;
var fractionalTime = totalhours + "." + fractionMinutes;
message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);
alert(message);
}
function validate(ctrl,whichField) {
if (!isNumeric(ctrl.value,ctrl)) {
alert("Please enter only numbers in this field");
ctrl.focus();
ctrl.value = "";
event.cancelBubble = true;
return false;
}
}
function roundIt(field,time)
{
var newVal = parseFloat(field.value).toFixed(2);
if ((newVal < 10) && (time != "")) {
field.value = "0" + newVal;
} else {
field.value = newVal;
}
}
</script>
</head>
<body>
<form name="frm">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr>
<td align="right">Start Time:</td>
<td><span class="row">
<input name="startTime" type="text" id="startTime" onChange="roundIt(this,'yes'); return validate(this,'startTime');">
</span></td>
</tr>
<tr>
<td align="right">End Time: </td>
<td><span class="row">
<input name="endTime" type="text" id="endTime" onChange="roundIt(this,'yes'); return validate(this,'endTime');">
</span></td>
</tr>
<tr>
<td align="right"> </td>
<td><a href="javascript:calculateHours()">calculate</a></td>
</tr>
</table>
</form>
</body>
</html>
any help will be much appreciated!!!!
eg: start time = 10.00
end time = 11.00
the difference should be 1.00, but its coming out as 1.10
starttime = 13.30
end time = 15.00
it should be 1.5 hours difference, yet it comes up with 1.15!
you can see it in action here: http://www.mattfacer.com/test.htm
here's the entire code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function calculateHours() {
//work out the fractional times
message = "";
if (document.frm.endTime.value == "") { endTime = "0.00"; } else { endTime = document.frm.endTime.value; }
if (document.frm.startTime.value == "") { startTime = "0.00"; } else { startTime = document.frm.startTime.value; }
if ((document.frm.endTime.value == "") || (document.frm.endTime.value == "")) {
message = "Could not calculate - Both start and end times must be entered";
}
//times
if (document.frm.startTime.value > document.frm.endTime.value) {
message = "Could not calculate - The start time cannot be greater than the end time";
}
var startArray = startTime.split(".");
var endArray = endTime.split(".");
//minutes
var startminutes = startArray[0]*60 + startArray[1]*1;
var endminutes = endArray[0]*60 + endArray[1]*1;
var totalminutes = endminutes - startminutes;
var fractionMinutes = (totalminutes/60)*100;
var totalhours = Math.floor(totalminutes / 60);
var totalminutes = totalminutes%60;
var fractionalTime = totalhours + "." + fractionMinutes;
message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);
alert(message);
}
function validate(ctrl,whichField) {
if (!isNumeric(ctrl.value,ctrl)) {
alert("Please enter only numbers in this field");
ctrl.focus();
ctrl.value = "";
event.cancelBubble = true;
return false;
}
}
function roundIt(field,time)
{
var newVal = parseFloat(field.value).toFixed(2);
if ((newVal < 10) && (time != "")) {
field.value = "0" + newVal;
} else {
field.value = newVal;
}
}
</script>
</head>
<body>
<form name="frm">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr>
<td align="right">Start Time:</td>
<td><span class="row">
<input name="startTime" type="text" id="startTime" onChange="roundIt(this,'yes'); return validate(this,'startTime');">
</span></td>
</tr>
<tr>
<td align="right">End Time: </td>
<td><span class="row">
<input name="endTime" type="text" id="endTime" onChange="roundIt(this,'yes'); return validate(this,'endTime');">
</span></td>
</tr>
<tr>
<td align="right"> </td>
<td><a href="javascript:calculateHours()">calculate</a></td>
</tr>
</table>
</form>
</body>
</html>
any help will be much appreciated!!!!