0

我有一张这样的桌子:

Hour 1 | Hour 2 | Difference
16:30    12:30     = ?
16:30    12:30     = ?
16:30    12:30     = ?

我想用jquery计算两列“Hour 1”和“Hour 2”之间的差异

4

2 回答 2

1

tr:gt(0)表示不包括最顶部的表格标题行

演示

如果您有第三列空白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);
});

如果您没有第三列空白td,则可以附加 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>');
});
于 2013-08-12T12:11:47.417 回答
0

我猜想获得时间差异的最短方法是:

diff = (Date.parse('2000-01-01T16:30:00')-Date.parse('2000-01-01T12:30:00'))/1000/60/60;

要获得以分钟为单位的差异,请/60从上述语句中删除最后一个。

根据需要替换上述字符串中的和16:3012:30

于 2013-08-12T12:02:33.363 回答