I have a table something like this :
Hour 1 | Hour 2 | Difference
16:30 12:30 = ?
16:30 12:30 = ?
16:30 12:30 = ?
And i want to calculate the difference between the two columns "Hour 1" and "Hour 2" with jquery
I have a table something like this :
Hour 1 | Hour 2 | Difference
16:30 12:30 = ?
16:30 12:30 = ?
16:30 12:30 = ?
And i want to calculate the difference between the two columns "Hour 1" and "Hour 2" with jquery
tr:gt(0)
means not including the top most table header row
if you have 3rd column blank td
$('table tr:gt(0)').each(function(){
var str_1 = $(this).find('td:nth(0)').text().split(':');
var str_2 = $(this).find('td:nth(1)').text().split(':')
var str_3 = (parseInt(str_1[0])-parseInt(str_2[0]))+':'+(parseInt(str_1[1])-parseInt(str_2[1]));
$(this).find('td:nth(2)').text(str_3);
});
if you don't have 3rd Column blank td
than you can append the td
$('table tr:gt(0)').each(function () {
var str_1 = $(this).find('td:nth(0)').text().split(':');
var str_2 = $(this).find('td:nth(1)').text().split(':')
var str_3 = (parseInt(str_1[0]) - parseInt(str_2[0])) + ':' + (parseInt(str_1[1]) - parseInt(str_2[1]));
$(this).append('<td>'+str_3+'</td>');
});
The shortest way to get the difference in hours i guess would be:
diff = (Date.parse('2000-01-01T16:30:00')-Date.parse('2000-01-01T12:30:00'))/1000/60/60;
To get the difference in minutes, remove the last /60
from the above statement.
Replace 16:30
and 12:30
in the above string as needed.