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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question on coverting time to a number

    Does anyone have any idea on how I can convert a time to a number. For example, 1:00 to 1 and 1:30 to 1.5 and so forth? I have drop down lists with the values being the time (12:00, 12:30, etc.) and I will need to calculate the number of hours, e.g. if dropdown list1 is 12:30 and dropdown list2 is 5:00, then it will be 4.5 hours.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Why not put the corresponding hour value in the option value attribute?

    <form>
    <select name="list1">
    <option value="1.0">1:00</option>
    <option value="1.5">1:30</option>
    <option value="2.0">2:00</option>
    ...
    </select>
    <select name="list2">
    <option value="4.0">4:00</option>
    <option value="4.5">4:30</option>
    <option value="5.0">5:00</option>
    ...
    </select>
    <input type="button" value="Compute" onclick="computeTime(this.form)">
    </form>

    Then just subtract the selected values.

    function computeTime(f){
    var fr = f.list1.options[f.list1.selectedIndex].value;
    var to = f.list2.options[f.list2.selectedIndex].value;
    var diff = to - fr;
    alert(diff);
    }

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried it this way, just for kicks... might work:
    Code:
    <script>
    
    var Time1, Time2, sDate, result;
    
    Time1 = "23:30"; // otherwise use "11:30 PM"
    
    Time2 = "11:00"; // otherwise use "11:00 AM"
    
    sDate = "1/1/70\u0020";
    
    result = Math.abs(((Date.parse(sDate + Time2) - Date.parse(sDate + Time1)) / 3600000).toFixed(2));
    
    alert(result);
    
    </script>
    hmm... ?


  •  

    Posting Permissions

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