Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Percentage of 2 times

Status
Not open for further replies.

mptwoadmin

Programmer
May 15, 2006
46
0
0
US
Hi All, I would like some assistance with the following code: I am try to get the percentage of 2 inputted times and have another field auto populate but I am not that familiar with JavaScript and "times",(i can get two numbers to work but not the times). Any help would be great or if someone could lead me in the right directions. (an example to get me started would also be helpful.

Both time vales are the time it has taken someone to complete a job.


<script type="text/javascript">
function calc(A,B,SUM) {
var one = Number(A);
var two = Number(document.getElementById(B).value);
document.getElementById(SUM).value = one + two;
}
</script>

<form>
<input style="text-align: right" name="sum1" id="op1"
onChange="calc(this.value,'op2','result')" readonly value="12:10:30"/>
<input name="sum2" style="text-align: right" id="op2"
onChange="calc(this.value,'op1','result')" value="07:10:30" />
<input name="sum" id="result" readonly style="text-align: right">
</form>


Thanks
 
Since both op1 and op2 are readonly, what's going to trigger the onchange event.

Also, because of the format of your time values, you're going to have a hard time adding or multiplying them without converting to total seconds first. The hh:mm:ss format doesn't convert to a Number object without some help from the programmer.

Lee
 
Sorry I entered the value in "OP2" just to show the format that should be inputted. Thanks any feed back is welcome. Also what format should be entered to make this easier? I need just the amount of hours minutes seconds worked.

thanks again

 
Think about it. How would you add the times together on paper using a pencil? Write the code for that.

Lee
 
Sorry If I knew I would not need to ask for a little assistance, I am not really well versed in JavaScript.

Thanks for your help anyway.

 
How would you add 2 times together on paper? Once you describe that process, it's not difficult to write the code to do that in JS.

Lee
 
Here is my dynamic solution...In case anyone else has use..It contains some Coldfusion Tags but just disregard and substitute your own.

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function calc(A,B,SUM) {

//Calc decimal for badge swipe time
var one =A;
var timePat = /^(\d{1,2}):(\d{2}):)(\d{2}))?(\s?(AM|am|PM|pm))?$/;
// Check to see if time is in valid format
var matchArray = one.match(timePat);
if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}
//Convert time to decimal
hour = (matchArray[1]);
minute = (matchArray[2])/60;
second = (matchArray[4])/3600;
sub=eval(hour)+eval(minute)+eval(second);

//Calc decimal for Other swipe time
var one2 =document.getElementById(B).value;
var timePat2 = /^(\d{1,2}):(\d{2}):)(\d{2}))?(\s?(AM|am|PM|pm))?$/;
var matchArray2 = one2.match(timePat2);
hour2 = (matchArray2[1]);
minute2 = (matchArray2[2])/60;
second2 = (matchArray2[4])/3600;
sub2=eval(hour2)+eval(minute2)+eval(second2);

//Divide time for percentage
tot_l = (sub2/sub)*100

//Output percentage in sum field to precesion and concatenate percentage sign.

var two = document.getElementById(SUM).value = tot_l.toPrecision(4) + "%";
}
// End -->
</script>




<td align="right"><cfoutput><font><input width="9" style="border: none"
style="text-align: right" name="sum1" id="op1_#get_data_1.name#"
onChange="calc(this.value,'op2_#get_data_1.name#','result_#get_data_1.name#')"
readonly
value="#numberformat(hours,'0')#:#numberformat(minutess,'00')#:#numberformat(secondss,'00')#"/></font></cfoutput></td>

<td align="right"><font><input width="9" name="sum2"
id="op2_#get_data_1.name#" style="text-align: right"
onChange="calc(this.value,'op1_#get_data_1.name#','result_#get_data_1.name#')"
value=""></font></td>

<td align="right"><font><input width="5"name="sum"
id="result_#get_data_1.name#" readonly style="border: none"
style="text-align: right"></font> </td>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top