Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## decimal time problem

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

Code:
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<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);

}
function validate(ctrl,whichField) {
if (!isNumeric(ctrl.value,ctrl)) {
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>

<body>
<form name="frm">
<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!!!!

• 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.

• 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

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

• 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.

Code:
```<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<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>
<body>
<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>```

• var totalhours = Math.floor(totalminutes / 60);
var totalminutes = totalminutes%60;
var fractionalTime = totalhours + "." + fractionMinutes;
these fractionMinutes must be totalminutes

• 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!

• then change the line to:

Code:
` var fractionalTime = totalhours + "." + totalminutes/0.6;`

• thanks lerura - thats working perfectly now!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•