...

View Full Version : decimal time problem



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">&nbsp;</td>
<td><a href="javascript:calculateHours()">calculate</a></td>
</tr>
</table>
</form>
</body>
</html>


any help will be much appreciated!!!!

rlemon
10-17-2005, 03:59 PM
The first problem i see is that you call a function 'isNumeric' and that funciton does not exist.

i'll get this working and re-post.

homerUK
10-17-2005, 04:30 PM
this is a cut down version of a much larger page - so i may have missed out some of the functions... .sorry!

here's the isNumeric function



function isNumeric(string, field, wipeField) {
re = /^\d+(\.\d+)?$/;
if (re.test(string)) {
return true;
} else {
return false;
}
}


Cheers :)

rlemon
10-17-2005, 06:02 PM
i used some concepts you had but i found it easier to just re-write the page. here it is working for me in FF. i havn't tested it for bugs fully yet so bare with me. as far as i can tell however there shouldn't be any problems with it.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/javascript">
function validate(id){
var e = document.getElementById(id);
var cv = e.value;
if(!cv.match("^[0-9]{1,2}[/:][0-9]{2}")){
// checks
var vfront = false; var vback = false;
if(cv.match("^[0-9]{1,2}")){ vfront = true; } // front
if(cv.match("[0-9]{2}$")){ vback = true; }
var output = '';
if(vfront == true){ output+=cv.match("^[0-9]{1,2}"); } else { output+='00'; }
output+=':';
if(vback == true){ output+=cv.match("[0-9]{2}$"); } else { output+='00'; }
e.value = output; // rebuild output
}
}
function calculateTime(id1,id2){ /* id2.value - id1.value */
var e1 = document.getElementById(id1); var e2 = document.getElementById(id2);
var e1_minutes = toMins(id1);
var e2_minutes = toMins(id2);
var outMins = e2_minutes - e1_minutes;
var total = fromMins(outMins);
var spOUT = document.getElementById('output');
spOUT.innerHTML = "The total time: "+total;

}
function toMins(id){
var e = document.getElementById(id);
var eA = e.value.split(':');
var eAM = Math.floor( parseInt(eA[0] * 60) + parseInt(eA[1]) );
return eAM;
}
function fromMins(mins){
hours = Math.floor(mins/60);
minutes = mins - (hours*60);
time = hours + ":" + minutes;
return time;
}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>StartTime:</td>
<td>
<input type="text" name="startTime" id="startTime" onChange="validate(this.id)" />
</td>
</tr>
<tr>
<td>EndTime:</td>
<td>
<input type="text" name="endTime" id="endTime" onChange="validate(this.id)" />
</td>
</tr>
<tr><td colspan="2"><input type="button" onClick="calculateTime('startTime','endTime')" value="calculate" /></td></tr>
<tr><td colspan="2"><span id="output"></span></td></tr>
</table>
</body>
</html>

Lerura
10-17-2005, 07:48 PM
var totalhours = Math.floor(totalminutes / 60);
var totalminutes = totalminutes%60;
var fractionalTime = totalhours + "." + fractionMinutes;
these fractionMinutes must be totalminutes

homerUK
10-17-2005, 08:58 PM
thanks for the replies...

I tried hte first suggestion - that works VERY nicely for working out the hours and minutes... but the thing is, I need this as a decimal.. .eg: 1 hour 45 minutes should be 1.75 hours

I tried changing the original code as lerura suggested... but that just shows the minutes.... so 2 hours 30 comes out as 2.30 when it should be 2.5

thanks very much for your help!

Lerura
10-17-2005, 09:05 PM
then change the line to:


var fractionalTime = totalhours + "." + totalminutes/0.6;

homerUK
10-18-2005, 10:58 AM
thanks lerura - thats working perfectly now!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum