...

View Full Version : Time Calculation



wizard210
02-11-2004, 05:43 PM
I have a form set up that has three fields in it.

First Field = Start Time
Second Field = End Time
Third Field = Elapsed Time

What I would like to do is take the Field 1 data and field 2 data to get field 3 data. Here is the fuction that i have to do this procedure.



<script language="javascript">
function calculatetime(){
var timeS = document.forms['computerlab'].elements['start_time'].value
var timeE = document.forms['computerlab'].elements['end_time'].value
var elapsed = timeE - timeS

document.forms['computerlab'].elements['elapsed_time'].value = elapsed;

if (timeS > timeE){
alert ("Start time cannot be this time!")};
}



</script>


The above code does what I want it to do but here is what I would really like for it to do. For example if timeS is 8:30 and timeE is 9:30. I want elapsed to show "1 hour". I think I can do this with an if/else statement but tha is a lot of code to write. I'm think there is a better way to accomplish this. Any body know of one.

Below is my form code for the three fields



<p>Start Time: <select size="1" name="start_time" id="start_time">
<option value="8.00">8:00 a.m</option>
<option value="8.15">8:15 a.m</option>
<option value="8.30">8:30 a.m</option>
<option value="8.45">8:45 a.m</option>
<option value="9.00">9:00 a.m</option>
<option value="9.15">9:15 a.m</option>
<option value="9.30">9:30 a.m</option>
<option value="9.45">9:45 a.m</option>
<option value="10.00">10:00 a.m</option>
<option value="10.15">10:15 a.m</option>
<option value="10.30">10:30 a.m</option>
<option value="10.45">10:45 a.m</option>
<option value="11.00">11:00 a.m</option>
<option value="11.15">11:15 a.m</option>
<option value="11.30">11:30 a.m</option>
<option value="11.45">11:45 a.m</option>
<option value="12.00">12:00 p.m</option>
<option value="12.15">12:15 p.m</option>
<option value="12.30">12:30 p.m</option>
<option value="12.45">12:45 p.m</option>
<option value="1.00">1:00 p.m</option>
<option value="1.15">1:15 p.m</option>
<option value="1.30">1:30 p.m</option>
<option value="1.45">1:45 p.m</option>
<option value="2.00">2:00 p.m</option>
<option value="2.15">2:15 p.m</option>
<option value="2.30">2:30 p.m</option>
<option value="2.45">2:45 p.m</option>
<option value="3.00">3:00 p.m</option>
</select>

End Time:<select size="1" name="end_time" id="end_time">
<option value="8.00">8:00 a.m</option>
<option value="8.15">8:15 a.m</option>
<option value="8.30">8:30 a.m</option>
<option value="8.45">8:45 a.m</option>
<option value="9.00">9:00 a.m</option>
<option value="9.15">9:15 a.m</option>
<option value="9.30">9:30 a.m</option>
<option value="9.45">9:45 a.m</option>
<option value="10.00">10:00 a.m</option>
<option value="10.15">10:15 a.m</option>
<option value="10.30">10:30 a.m</option>
<option value="10.45">10:45 a.m</option>
<option value="11.00">11:00 a.m</option>
<option value="11.15">11:15 a.m</option>
<option value="11.30">11:30 a.m</option>
<option value="11.45">11:45 a.m</option>
<option value="12.00">12:00 p.m</option>
<option value="12.15">12:15 p.m</option>
<option value="12.30">12:30 p.m</option>
<option value="12.45">12:45 p.m</option>
<option value="1.00">1:00 p.m</option>
<option value="1.15">1:15 p.m</option>
<option value="1.30">1:30 p.m</option>
<option value="1.45">1:45 p.m</option>
<option value="2.00">2:00 p.m</option>
<option value="2.15">2:15 p.m</option>
<option value="2.30">2:30 p.m</option>
<option value="2.45">2:45 p.m</option>
<option value="3.00">3:00 p.m</option>
</select>
Elapsed Time:<input type="text" name="elapsed_time" size="15">

Willy Duitt
02-11-2004, 07:13 PM
Edit: Removed script. Did not work as intended.

.....Willy

Willy Duitt
02-11-2004, 08:20 PM
Fundemental flaw in the way my method deducts 11:30am from 8:45am. I'll look at it again later when I get more time unless someone else comes along with a better way. :o

.....Willy

Willy Duitt
02-11-2004, 11:32 PM
Used more logic this time around.
Try this:


<script type="text/javascript">
<!--//
function calculatetime(){
var form = document.forms['computerlab'].elements;
var timeS = parseFloat(form['start_time'].value);
var timeE = parseFloat(form['end_time'].value);
var elapsed = parseFloat(timeE - timeS).toFixed(2).toString().split('\.');
var minutes = 'and '+(elapsed[1]/100*60)+' minutes';
var hours = elapsed[0]+' Hours ';

if(elapsed[0] == 0){hours = ''; minutes = (elapsed[1]/100*60)+' minutes'};
if(elapsed[0] == 1){hours = elapsed[0]+' Hour '};
if(elapsed[1] == 0){minutes = ''};

if (timeS >= timeE){
alert ("Start time cannot be this time!");
form['start_time'].focus();
return false};

form['elapsed_time'].value = hours+minutes;
}
//-->
</script>
</HEAD>

<BODY>
<form name="computerlab">
<p>Start Time: <select size="1" name="start_time" id="start_time">
<option value="8.00">8:00 a.m</option>
<option value="8.25">8:15 a.m</option>
<option value="8.50">8:30 a.m</option>
<option value="8.75">8:45 a.m</option>
<option value="9.00">9:00 a.m</option>
<option value="9.25">9:15 a.m</option>
<option value="9.50">9:30 a.m</option>
<option value="9.75">9:45 a.m</option>
<option value="10.00">10:00 a.m</option>
<option value="10.25">10:15 a.m</option>
<option value="10.50">10:30 a.m</option>
<option value="10.75">10:45 a.m</option>
<option value="11.00">11:00 a.m</option>
<option value="11.25">11:15 a.m</option>
<option value="11.50">11:30 a.m</option>
<option value="11.75">11:45 a.m</option>
<option value="12.00">12:00 p.m</option>
<option value="12.25">12:15 p.m</option>
<option value="12.50">12:30 p.m</option>
<option value="12.75">12:45 p.m</option>
<option value="13.00">1:00 p.m</option>
<option value="13.25">1:15 p.m</option>
<option value="13.50">1:30 p.m</option>
<option value="13.75">1:45 p.m</option>
<option value="14.00">2:00 p.m</option>
<option value="14.25">2:15 p.m</option>
<option value="14.50">2:30 p.m</option>
<option value="14.75">2:45 p.m</option>
<option value="14.00">3:00 p.m</option>
</select>

End Time:<select size="1" name="end_time" id="end_time" onchange="calculatetime()">
<option value="8.00">8:00 a.m</option>
<option value="8.25">8:15 a.m</option>
<option value="8.50">8:30 a.m</option>
<option value="8.75">8:45 a.m</option>
<option value="9.00">9:00 a.m</option>
<option value="9.25">9:15 a.m</option>
<option value="9.50">9:30 a.m</option>
<option value="9.75">9:45 a.m</option>
<option value="10.00">10:00 a.m</option>
<option value="10.25">10:15 a.m</option>
<option value="10.50">10:30 a.m</option>
<option value="10.75">10:45 a.m</option>
<option value="11.00">11:00 a.m</option>
<option value="11.25">11:15 a.m</option>
<option value="11.50">11:30 a.m</option>
<option value="11.75">11:45 a.m</option>
<option value="12.00">12:00 p.m</option>
<option value="12.25">12:15 p.m</option>
<option value="12.50">12:30 p.m</option>
<option value="12.75">12:45 p.m</option>
<option value="13.00">1:00 p.m</option>
<option value="13.25">1:15 p.m</option>
<option value="13.50">1:30 p.m</option>
<option value="13.75">1:45 p.m</option>
<option value="14.00">2:00 p.m</option>
<option value="14.25">2:15 p.m</option>
<option value="14.50">2:30 p.m</option>
<option value="14.75">2:45 p.m</option>
<option value="14.00">3:00 p.m</option>
</select>
Elapsed Time:<input type="text" name="elapsed_time" size="25">
</form>

.....Willy

Edit: Shortened script somewhat.

wizard210
02-12-2004, 05:07 AM
Awesome Willy. Thanks for the code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum