List Info

Thread: Re: Re: How to sum up float values and convert into exact hours and minutes




Re: Re: How to sum up float values and convert into exact hours and minutes
country flaguser name
India
1969-12-31 18:00:00

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_semvalyahoo.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&quot;][&quot;time"; + i].value;
alert(current);
//does the time field value contain a ":&quot;, ".&quot;, or something else?
//if it contains a ":&quot; we can assume that the
//value represents hours and minutes
if(current.indexOf( ":&quot;) != -1)
{
parts = current.split( ":&quot;);
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 ".&quot; then we need to convert
//the second part from fractional hours to minutes
if(current.indexOf( ".&quot;) != -1)
{
parts = current.split( ".&quot;);
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&quot;+totalHours);
alert(&quot;TotalMinutes"+totalMinutes);
}
if(totalMinutes < 10)
totalMinutes ="0&quot;;
document.forms["timeForm"]["timeTotal&quot;].value=totalHours+":&quot;+totalMinutes;

}
&lt;/script&gt;
</head>
<body>;
Let's suppose that the HTML for that part of your form looks like this:
&lt;form name=";timeForm&quot; >
<;p>Monday: <input name=";time0"; onChange=&quot;calc()"/>;</p>
<p>;Tuesday: <input name=";time1"; onChange=&quot;calc()"/>;</p>
<p>;Wednesday: <input name=";time2"; onChange=&quot;calc()"/>;</p>
<p>;Thursday: <input name=";time3"; onChange=&quot;calc()"/>;</p>
<p>;Friday: <input name=";time4"; onChange=&quot;calc()"/>;</p>
<p>;Total: <input name=";timeTotal&quot; /></p>
<;input type=submit>
<;/form>
</body>
</html>

Regards,
Sunitha
Jon Stephens < jon%40hiveminds.net">jonhiveminds.net> wrote:

> How to sum up float values and convert into exact hours and minutes
> Posted by: "sunitha semval&quot; ss_semval%40yahoo.co.in">ss_semvalyahoo.co.in ss_semval
> Date: Tue Feb 27, 2007 9:32 pm ((PST))
>
> Hi Jon,
>;
> Thank you very much for the explanation.
>
> I am stuck at other point of my coding,
>
> In textboxes hours and mins are entered like
>;
> Txt1: 8.30 Txt2: 8:05 Txt3: 8.45 Txt4:8.25
>
> I am adding the total hours in Javascript and printing back in Total
&gt; Columns Text Field.
&gt;
> As the input is in hours and mins, I want the total to be incremented
> by 1 whenever mins reach 60 secs.
&gt;
> Please let me know how to convert Sum of Float values to exact hours
&gt; and mins.

Let's suppose that the HTML for that part of your form looks like this:

<form name=";timeForm&quot;>
<p>Monday: <input name=";time0"; /></p>
<;p>Tuesday: <input name=";time1"; /></p>
<;p>Wednesday: <input name=";time2"; /></p>
<;p>Thursday: <input name=";time3"; /></p>
<;p>Friday: <input name=";time4"; /></p>

<p>Total: <input name=";timeTotal&quot; /></p>
<;/form>

Here's the plan of action that I'd follow:

# first let's set up some convenience variables

var numberOfTimeInputs = 5;
var myForm = document.timeForm;

var current = 0;

var total = 0;
var totalHours = 0;
var totalMinutes = 0;

var parts;
var temp;

# for each of the "time" fields...

for(var i = 0; i < numberOfTmeInputs; i++)
{
# get the value in the current time field
current = myForm[&quot;time&quot; + i].value;

# does the time field value contain a ":&quot;, ".&quot;, or something else?

# if it contains a ":&quot; we can assume that the
# value represents hours and minutes
if(current.indexOf(&quot;:") != -1)
{
parts = current.split(":");

totalHours += (parts[0] + 0);
totalMinutes += (parts[1] + 0);

# /me finds himself wishing that JS had a list() operator... ;)
}
else
{
# if the value contains a ".&quot; then we need to convert
# the second part from fractional hours to minutes
if(current.indexOf(&quot;.") != -1)
{
parts = current.split(".");

totalHours += (parts[0] + 0);
totalMinutes += Math.round((parts[1] + 0) * 60);
}
else
{
# invalid input, maybe? need to handle that
;
}
}
}

# if the total number of minutes is > 60 then we need to
# (a) to extract the number of hours from it and add that to
# the total number of hours, and (b) to subtract the minutes
# in excess of 60 from the total number of minutes

if(totalMinutes > 60)
{
totalHours += Math.floor(totalMinutes / 60);
totalMinutes %= 60;
}

# pad the minutes with a 0 if it's < 10
if(totalMinutes < 10)
totalMinutes = "0&quot; = totalMinutes;

# now display the result

timeForm.timeTotal.value = totalHours + ":&quot; + totalMinutes;

I can think of at least 2 other ways to do this, but hopefully this will
get you started - I have to go work in my real job now.

cheers

jon.

>
> Thanks & Regards, Sunitha.

--
This message has not been scanned for viruses.

Since I do not use a Microsoft operating
system or software, and use only plaintext
for email, there is little need for me to do so.

---------------------------------
Here’s a new way to find what you're looking for - Yahoo! Answers

[Non-text portions of this message have been removed]


---------------------------------
Here’s a new way to find what you're looking for - Yahoo! Answers

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )