SecondaryTime的值 - PrimaryTime的值
,答案将放在Sp textfield中。
时间格式为HH:MM:SS
HELP !! :))
<SCRIPT language="javascript">
function calculate(time1,time2){
var diff = Math.abs(time1 - time2);
document.form.Sp.value = diff;
}
</script>
<form name="form1" method="post" action="">
<label for="PrimaryTime"></label>
<input type="text" name="PrimaryTime" id="PrimaryTime">
<label for="SecondaryTime"></label>
<input type="text" name="SecondaryTime" id="SecondaryTime" onFocus = "calculate(this.form.SecondaryTime.value, this.form.PrimaryTime.value)>
<label for="Sp"></label>
<input name="Sp" type="text" id="Sp" readonly="readonly">
</form>
答案 0 :(得分:0)
我没有改进,我只是让它们起作用:
<SCRIPT language="javascript">
function calculate(time1,time2){
time1_split = time1.split(":");
time2_split = time2.split(":");
var diff = Math.abs(time1_split[0] - time2_split[0]) + ":" + Math.abs(time1_split[1] - time2_split[1]) + ":" + Math.abs(time1_split[2] - time2_split[2]);
form1.Sp.value = diff;
}
</script>
<form name="form1" method="post" action="">
<label for="PrimaryTime"></label>
<input type="text" name="PrimaryTime" id="PrimaryTime">
<label for="SecondaryTime"></label>
<input type="text" name="SecondaryTime" id="SecondaryTime" onFocus = "calculate(this.form.SecondaryTime.value, this.form.PrimaryTime.value)">
<label for="Sp"></label>
<input name="Sp" type="text" id="Sp" readonly="readonly">
</form>
答案 1 :(得分:0)
要正确进行计算,您需要将时间转换为某个公共单位(比如秒),减去两个,然后转换回h:m:s,例如
// difference in time in format hh:mm:ss
function diffTime(t0, t1) {
t0 = t0.split(':');
t1 = t1.split(':');
var diff = (+t0[0] - +t1[0])*3600 + (+t0[1] - +t1[1])*60 + +t0[2] - +t1[2];
var sign = diff<0? '-':'';
diff = Math.abs(diff);
var h = diff/3600 | 0;
var m = (diff%3600) / 60 | 0;
var s = (diff%60);
function addZ(n) {
return (n<10? '0':'') + n;
}
return sign + addZ(h) + ':' + addZ(m) + ':' + addZ(s);
}