By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

How can I calculate duration in seconds for more rows from two times?

P: 9
Expand|Select|Wrap|Line Numbers
  1. How can I calculate duration in seconds for more rows from two times?
  2.  
  3.  
  4. <!--Time calculation-->
  5.  
  6.  
  7. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  8. <script>
  9. $(document).ready(function() {
  10.  //this calculates values automatically
  11.  sum();
  12.  $("#start_time, #end_time").on("keydown keyup", function() {
  13.  sum();
  14.  });
  15. });
  16.  
  17. function sum() {
  18.  var start_time = document.getElementById('start_time').value;
  19.  var end_time = document.getElementById('end_time').value;
  20.  var result = parseInt(end_time) - parseInt(start_time);
  21.  
  22.  if (!isNaN(result)) {
  23.  document.getElementById('sum').value = result;
  24.  
  25.  }
  26.  }
  27. </script>
  28.  
  29.  
  30. <!--add row-->
  31.  
  32.    <script>
  33. function addRow(tableID) {
  34.     var table = document.getElementById(tableID);
  35.     var rowCount = table.rows.length;
  36.         var row = table.insertRow(rowCount);
  37.         var colCount = table.rows[0].cells.length;
  38.         row.id = 'row_'+rowCount;
  39.         for (var i = 0; i < colCount; i++) {
  40.             var newcell = row.insertCell(i);
  41.             newcell.outerHTML = table.rows[0].cells[i].outerHTML;            
  42.         }
  43.     }
  44. function deleteRow(tableID) {
  45.     var table = document.getElementById(tableID);
  46.     var rowCount = table.rows.length;
  47.     for (var i = 0; i < rowCount; i++) {
  48.         var row = table.rows[i];
  49.         var chkbox = row.cells[0].childNodes[0];
  50.         if (null !== chkbox && true === chkbox.checked) {
  51.             if (rowCount <= 1) { // limit the user from removing all the fields
  52.                 alert("Cannot Remove all the Passenger.");
  53.                 break;
  54.             }
  55.             table.deleteRow(i);
  56.             rowCount--;
  57.             i--;
  58.    }
  59.     }
  60.     }
  61.  
  62. </script>
  63.  
  64.  
  65.  
  66.  
  67. <!--form-->
  68.  
  69.  
  70. <form method="post" action="action_entry_history.php" name="form">
  71.  
  72.     <TABLE id="dataTable" width="100%" border="1" align="center">
  73.  
  74. <tr id='row_0'>
  75. <td><INPUT type="checkbox" size="5" name="chk"/></td>
  76.  
  77. <td><input type="time" name="start_time[]" size="5"  id="start_time"  step="1" placeholder="HH:MM:SS"/></td>
  78. <td><input type="time" name="end_time[]" size="5"  id="end_time" step="1" placeholder="HH:MM:SS"/></td>
  79. <td><input type="text" id="sum"  size="5" name="duration[]" placeholder="Duration" readonly></td>
  80.  
  81.     </tr>    
  82. </table>
  83.         <INPUT type="button" value="Add Row" onClick="addRow('dataTable')" />
  84.         <INPUT type="button" value="Delete Row" onClick="deleteRow('dataTable')" />
  85.         <input type="submit" value="Submit" name="submit">
  86.     </form>
  87.  
  88.  
  89.  
Attached Images
File Type: jpg Untitled1.jpg (14.2 KB, 53 views)
Sep 21 '16 #1
Share this Question
Share on Google+
1 Reply


P: 9
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <input type="time" step="1" id="startTimeInput"  name="start_time" value=""><br>     <!--   write as like       02:31:55   -->
  3. <input  type="time" step="1" id="endTimeInput"  name="end_time" value=""><br>
  4. <button id="calcBtn">Seconds</button><br>
  5. <div id="result"></div>
  6. <script type="text/javascript" src="js/jquery.js"></script>
  7. <script type="text/javascript">
  8. document.getElementById('calcBtn').onclick = function(){
  9.     var startTime = document.getElementById('startTimeInput').value;
  10.     var endTime = document.getElementById('endTimeInput').value;
  11.   var result = getDeltaSeconds(startTime, endTime);
  12.   document.getElementById('result').innerHTML = result + ' sec';
  13. }
  14. function getDeltaSeconds(startTime, endTime){
  15.     var startTimeSeconds = getSeconds(startTime);
  16.   var endTimeSeconds = getSeconds(endTime);
  17.   return endTimeSeconds - startTimeSeconds;    
  18. }
  19. function getSeconds(timeString){
  20.     var parts = timeString.split(' ');
  21.   var time = parts[0];
  22.   var ampm = parts[1];
  23.   var timeParts = time.split(':');
  24.   var seconds = Number(timeParts[2]) + Number(timeParts[1]*60) + Number(timeParts[0]*60*60);
  25.   return ampm == 'AM' ? seconds : seconds + 12*60*60;
  26. }
  27. </script>
  28. </body>
  29.  
  30.  
Oct 4 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.