-1

SecondaryTime 的值 - PrimaryTime 的值

答案将放在 Sp 文本字段中。

时间格式为 HH:MM:SS

帮助!!:))

<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>
4

2 回答 2

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>
于 2012-09-28T03:08:42.110 回答
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);
}
于 2012-09-28T03:56:37.983 回答