| 项目搜索 |
| |
|
代码片段库:
查看代码片段
浏览
| 提交新的代码片段
| 创建代码包
时间互换器
|
类型:
Full Script |
类别:
HTML Manipulation
|
许可证:
GNU General Public License |
语言:
JavaScript
|
描述:
一个小时(带小数)与小时分秒互相转换的小程序,有时会有用的
|
该代码片段的版本系列:
片段ID |
下载版本 |
提交时间 |
提交人 |
删除 |
4705 | 1.0 | 2003-10-14 15:36 | cbd001 | |
点击"下载版本"来下载该代码片段.
最新版本的代码片段: 1.0
脚本说明:
把如下代码加入<body>区域中
<SCRIPT LANGUAGE="JavaScript">
<!-- 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>小时<br>
<input type=text name=min size=3 maxlength=3>分<br>
<input type=text name=sec size=3 maxlength=3>秒
</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);">
<input type=button name=toTime value="Hours to Time" onClick="makeTime(this.form);">
</td>
</tr>
</table>
</form>
如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来. |
|