473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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, 86 views)
Sep 21 '16 #1
1 1147
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

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

Similar topics

2
by: Jeremy Chapman | last post by:
Populating a typed dataset from xml document: I created an xml schema (attached below), generated a typed dataset from it, and then programatically I tried to populate the typed dataset by...
0
by: Tu-Thach | last post by:
TimeSpan span = aEnd.Subtract(aStart); System.Console.WriteLine(span.TotalMilliseconds); Tu-Thach >-----Original Message----- >I want to calculate the time it takes to execute a function is...
2
by: Asad | last post by:
Hello, I need to know how I can calculate the number of seconds between two time stamps, e.g. if I have the following (using ToLongTimeString) 8:28:21 PM and 9:02:33 PM
6
by: Kwok | last post by:
Hi All, I want to make the longest string value (it probables more than 300 words.) to more rows to print it using printDocument. How do i do it like WORD that it auto cut string value at...
1
by: bazzer | last post by:
i have a webform which is connecting to a microsoft access database. the database contains info about dates and times of films showing in cinemas. im trying to get the times of a certain film from...
3
by: baka | last post by:
Dear Sir/madam Here i am having some one sql which returns more rows than the required rows after comaparing tables simple sql statement will be lokk like SELECT t.empcd, s.daicd, s.chucd,...
3
by: dejavu | last post by:
Can any one help me with this question : Write a menu based program to calculate the sum of rows and columns of an multidimensional array of size , using function ? please help !!!
3
archulu
by: archulu | last post by:
hai this is archulu. i am new to domapi.now my problem is in my project i am taken 2 domapi datepickers. what i am want Calculate duration between two dates which i am selected in that datepicker. ...
13
by: nma | last post by:
Hi How do I clculate time duration if I have a column like this 2008-04-19 15:00:55.000 2008-04-19 15:01:07.000 2008-04-19 16:33:55.000 2008-04-19 16:35:07.000 2008-04-19 16:37:00.000...
5
by: Mirku | last post by:
Hello! I have a table in a database where there is a column called DATE (datetime type)that logs the date/time of an event.The first index of the table it represent the start date/time and the last...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.