Hi Jon,
1) The following code works perfectly if time is entered as 8.0 or 8:00 but if only 8 is entered I even want this to be added to the total time.
2) Minutes are taken as 8.5 even 8.05 gets converted to 8.5.
I want it to show as 8.05.
3) Same thing, when we enter 8.00 it becomes 8.0 which I want it to show as 8.00.
Please assist,
Thanks & Regards,
Sunitha.
sunitha semval < ss_semval%40yahoo.co.in">ss_semval
yahoo.co.in> wrote:
Hi Jon,
Thanks for your advice on the following:
But I have tried to run the code, and I was getting few errors which I tried to debug and now I am fixed at a point and unable to solve it...
I am sending the source code..
Errors:
1) It adds 8:30+0 =8:30
8:30+9:20= it adds 8:30+8:30+9:20 (ie the previous value also)
and minutes is coming as ... for 8:30 ---- it says 030
9:50 --- 050
When I tried to calculate following values the result is as follows:
9:303;8:10+5:10+5:10=501710:30
Please advise..
<html>
<head>
<script language='javascript'>
var numberOfTimeInputs = 5;
var current = 0;
var total = 0;
var totalHours = 0;
var totalMinutes = 0;
var parts;
var temp;
function calc()
{
// for each of the "time" fields...
totalHours=0;
for(var i = 0; i<numberOfTimeInputs; i++)
{
//get the value in the current time field
current = document.forms["timeForm"]["time" + i].value;
alert(current);
//does the time field value contain a ":", ".", or something else?
//if it contains a ":" we can assume that the
//value represents hours and minutes
if(current.indexOf( ":") != -1)
{
parts = current.split( ":");
alert(parts);
var i1=parseInt(parts[0]);
alert(i1);
totalHours+=i1;
//totalHours += (parts[0] + 0);
alert(totalHours);
var j=parseInt(parts[1]);
totalMinutes += j+0;
alert(totalMinutes);
//me finds himself wishing that JS had a list() operator... ;)
}
else
{
}
//if the value contains a "." then we need to convert
//the second part from fractional hours to minutes
if(current.indexOf( ".") != -1)
{
parts = current.split( ".");
alert(parts);
totalHours += (parts[0] + 0);
alert(totalHours);
totalMinutes += Math.round(( parts[1] + 0) * 60);
alert(totalMinutes);
}
else
{
// invalid input, maybe? need to handle that
}
}
if(totalMinutes>=60)
{
totalHours += Math.floor((totalMinutes)/60);
totalMinutes %= 60;
alert("TotalHours"+totalHours);
alert("TotalMinutes"+totalMinutes);
}
if(totalMinutes < 10)
totalMinutes ="0";
document.forms["timeForm"]["timeTotal"].value=totalHours+":"+totalMinutes;
}
</script>
</head>
<body>
Let's suppose that the HTML for that part of your form looks like this:
<form name="timeForm" >
<p>Monday: <input name="time0" onChange="calc()"/></p>
<p>Tuesday: <input name="time1" onChange="calc()"/></p>
<p>Wednesday: <input name="time2" onChange="calc()"/></p>
<p>Thursday: <input name="time3" onChange="calc()"/></p>
<p>Friday: <input name="time4" onChange="calc()"/></p>
<p>Total: <input name="timeTotal" /></p>
<input type=submit>
</form>
</body>
</html>
Regards,
Sunitha
Jon Stephens < jon%40hiveminds.net">jon
hiveminds.net> wrote:
> How to sum up float values and convert into exact hours and minutes
> Posted by: "sunitha semval" ss_semval%40yahoo.co.