javascript - How to add two input time values and display it in another input box? -


i getting 2 durations, current time , previous time user. now, want calculate total time show on third textbox.

<p><span>current duration</span><input id="txt1" onblur="sum();" type="text" autocomplete="off" name="current_duration" value="" /></p> <p><span>previous duration</span><input id="txt2" onblur="sum();" type="text" autocomplete="off" name="previous_duration" value="" /></p> <p><span>total duration</span><input id="txt3" type="text" readonly autocomplete="off" name="total_duration" value="" /></p> <script>      function sum() {         var txtfirstnumbervalue = document.getelementbyid('txt1').value;         var txtsecondnumbervalue = document.getelementbyid('txt2').value;          var result = parseint(txtfirstnumbervalue) + parseint(txtsecondnumbervalue);         if (!isnan(result)) {             document.getelementbyid('txt3').value = result;          }     } </script> 

how can implement same? can guys me out?

assumning separator between time , minutes '.', work. if separator needed, replace character in totime() , fromtime()

<p><span>current duration</span><input id="txt1" onblur="sum();" type="text" autocomplete="off" name="current_duration" value="" /></p> <p><span>previous duration</span><input id="txt2" onblur="sum();" type="text" autocomplete="off" name="previous_duration" value="" /></p> <p><span>total duration</span><input id="txt3" type="text" readonly autocomplete="off" name="total_duration" value="" /></p>  <script> function sum() {     var txtfirstnumbervalue = document.getelementbyid('txt1').value;     var txtsecondnumbervalue = document.getelementbyid('txt2').value;      var result = fromtime(txtfirstnumbervalue) + fromtime(txtsecondnumbervalue);      if (!isnan(result)) {         document.getelementbyid('txt3').value = totime(result);     } }  function fromtime(time) {     var timearray = time.split('.');     var hours = parseint(timearray[0]);     var minutes = parseint(timearray[1]);      return (hours * 60) + minutes; }  function totime(number) {     var hours = math.floor(number / 60);     var minutes = number % 60;      return hours + "." + (minutes <= 9 ? "0" : "") + minutes; } </script> 

jsfiddle


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -