-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

計算を正しく行うには、時間を何らかの一般的な単位 (秒など) に変換し、2 つを減算してから、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 に答える