...

# Question on coverting time to a number

ABC123
03-22-2004, 07:32 AM
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.

glenngv
03-22-2004, 08:16 AM
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;
}

swmr
03-22-2004, 09:44 AM
I tried it this way, just for kicks... might work:

<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));