PDA

View Full Version : Function addvalues help



hugomax
08-14-2007, 10:40 AM
Can someone please advise, been looking for a code on the internet to convert minutes to hours in human time and not decimal. Searched for a long time and finally have came accross a script that does what I want but have had to make a few alterations to make it look the way i want..but the only problem is that when I click calculate it keeps adding the total. Could someone please take a look at the script and show me a way to just calculate once and not keep adding up (so..when you hit calculate it gives one answer, but the answer wont change until you hit reset) I know this is basic stuff to most coders, but I just cant do it. Thanks for any help...:) Heres the code I have.

<body>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function addValues (value) {
var myForm = document.timeCalculator;

if(myForm.total_time_hidden.value.length > 0){
var the_hours = parseInt(myForm.total_time_hidden.value.substring(0,myForm.total_time_hidden.value.indexOf(":")));
var the_minutes = parseInt(myForm.total_time_hidden.value.substring(myForm.total_time_hidden.value.indexOf(":")+1,myForm.total_time_hidden.value.lastIndexOf(":")));
var the_seconds = parseInt(myForm.total_time_hidden.value.substring(myForm.total_time_hidden.value.lastIndexOf(":")+1,myForm.total_time_hidden.value.length));
}
else{
var the_hours = 0;
var the_minutes = 0;
var the_seconds = 0;
}

var form_hours = parseFloat(myForm.frm_hours.value);
var form_minutes = parseFloat(myForm.frm_minutes.value);
var form_seconds = parseFloat(myForm.frm_seconds.value);
if (isNaN(form_hours)) {form_hours = 0;}
if (isNaN(form_minutes)) {form_minutes = 0;}
if (isNaN(form_seconds)) {form_seconds = 0;}
myForm.frm_hours.value = form_hours;
myForm.frm_minutes.value = form_minutes;
myForm.frm_seconds.value = form_seconds;

form_seconds = (form_hours * 60 * 60) + (form_minutes * 60) + form_seconds;
form_hours = 0; form_minutes = 0;
while(form_seconds >= 60){form_seconds = form_seconds - 60;form_minutes++;}
while(form_minutes >= 60){form_minutes = form_minutes - 60;form_hours++;}

the_hours = value * form_hours + the_hours;
the_minutes = value * form_minutes + the_minutes;
the_seconds = value * form_seconds + the_seconds;

while(the_seconds >= 60){the_seconds = the_seconds - 60;the_minutes++;}
while(the_minutes >= 60){the_minutes = the_minutes - 60;the_hours++;}
while(the_seconds < 0){the_seconds += 60;the_minutes--;}
while(the_minutes < 0){the_minutes += 60;the_hours--;}

the_seconds = Math.round(the_seconds *10000)/10000;

myForm.total_time_hidden.value = "" + the_hours + ":" + the_minutes + ":" +the_seconds;
myForm.total_time.value = "" + padout(the_hours) + ":" + padout(the_minutes) + ":" +padout(the_seconds);

var tempDecimal = the_hours + (the_minutes / 60) + (the_seconds / 60 / 60);
tempDecimal = Math.round(tempDecimal *10000)/10000;
myForm.total_time_decimal.value = "" + tempDecimal + " hours";
}

function resetValues(){
var myForm = document.timeCalculator;

myForm.frm_hours.value = "";
myForm.frm_minutes.value = "";
myForm.frm_seconds.value = "";

myForm.total_time.value = "00:00:00";
myForm.total_time_hidden.value = "0:0:0";
myForm.total_time_decimal.value = "0 hours";
}

function padout(number) { return (number < 10) ? '0' + number : number; }

// -->
</script>

<form name="timeCalculator">
<input type="hidden" name="total_time_hidden" value="0:0:0">
<div align="left"><left><table border="0">
<tr>
<td colspan="3"><font color="#800000">Enter Minutes to Convert to Hours:</font></td>


<td align="left">
<input TYPE="hidden" SIZE="7" NAME="frm_hours"></td>
<td align="left">
<input TYPE="text" NAME="frm_minutes" SIZE="7"></td>
<td align="center">
<input TYPE="hidden" NAME="frm_seconds" SIZE="7">
<td colspan="0" align="left"><a href="javascript:addValues(1)">Calculate</a>

<td colspan="3"><input type="text" name="total_time" size="10" maxlength="10" value="00:00:00" READONLY></td>

<td colspan="3">Decimal: <input type="text" name="total_time_decimal" size="15" maxlength="10" value="0 hours" READONLY><a href="javascript:resetValues();">Reset</a></td>
</tr>
</table>
</center></div>
</form>



</body>
</html>

hugomax
08-14-2007, 01:14 PM
:cool:PROBLEM SOLVED!!!...The solution was staring me in the eyes all along.

Twey
08-14-2007, 03:36 PM
To convert minutes into hours and minutes:
var manyminutes = 375,
hours = Math.floor(manyminutes / 60), // 6
minutes = manyminutes &#37; 60, // 15
strrep = hours + " hours, " + minutes + " minutes." // 6 hours, 15 minutes.