Script Categories













Equivalents >>> Time Converter.

Allows for the conversion back and forth from hours, minutes, and seconds to fractional hours. Input validation also ensures the time input does not contain invalid characters. Definitely a time-saver for time-related record keeping.

Time: Hours
Minutes
Seconds
Hours:

Add the below code to the <body> section of your page:

<script language="javascript" type="text/javascript">
/* Visit http://www.yaldex.com/ for full source code
and get more free JavaScript, CSS and DHTML scripts! */
<!-- Begin
function checkInt(str) {
if (!str) return 0;
var ok = "";
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i+1);
if (ch < "0" || "9" < ch) {
alert("Only integer input is allowed!\n\n"
+ parseInt(ok) + " will be used because '"
+ str + "' is invalid.\nYou may correct "
+ "this entry and try again.");
return parseInt(ok);
}
else ok += ch;
}
return parseInt(str);
}

function checkDecimal(str) {
if (!str) return 0;
var ok = "";
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i+1);
if ((ch < "0" || "9" < ch) && ch != '.') {
alert("Only numeric input is allowed!\n\n"
+ parseFloat(ok) + " will be used because '"
+ str + "' is invalid.\nYou may correct "
+ "this entry and try again.");
return parseFloat(ok);
}
else ok += ch;
}
return str;
}

function makeHours(form) {
hour = (checkInt(form.hour.value)); // validates input
min = (checkInt(form.min.value));   // sets invalid input to 0
sec = (checkInt(form.sec.value));
return (hour + min/60 + sec/3600);
}

function makeTime(form) {
num = (checkDecimal(form.hourtotal.value)); // validates input
if (num) {
form.hour.value = parseInt(num);
   num -= parseInt(num); num *= 60;
form.min.value = parseInt(num);
   num -= parseInt(num); num *= 60;
form.sec.value = parseInt(num);
   }
}
//  End -->
</script>
<form name="timeconverter">
<table border=1>
<tr>
<td>
Time:</td>
<td>
<input type=text name=hour size=3 maxlength=3> Hours<br>
<input type=text name=min size=3 maxlength=3> Minutes<br>
<input type=text name=sec size=3 maxlength=3> Seconds
</td>
</tr>

<tr>
<td>
Hours:</td>
<td>
<input type=text name=hourtotal size=10>
</td>
</tr>

<tr>

<td colspan=2 align=center>
<input type=button nametoHours value="Time to Hours" onClick="this.form.hourtotal.value=makeHours(this.form);">
<br>
<input type=button name=toTime value="Hours to Time" onClick="makeTime(this.form);">
</td>
</tr>
</table>
</form>

JavaScript Editor Get Advanced
JavaScript and Ajax Editor,
Validator and Debugger!

1st JavaScript Editor.



Code was highlighted by 1st JavaScript Editor (The Best JavaScript Editor!).




©