Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time Calculation

    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.

    Code:
    <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

    Code:
          <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">

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Edit: Removed script. Did not work as intended.

    .....Willy
    Last edited by Willy Duitt; 02-12-2004 at 12:19 AM.

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    .....Willy

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Used more logic this time around.
    Try this:
    Code:
    <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.
    Last edited by Willy Duitt; 02-12-2004 at 12:35 AM.

  • #5
    New Coder
    Join Date
    Jan 2004
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome Willy. Thanks for the code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •