SUM& jquery数据表中的TIME平均值

时间:2018-01-04 05:24:17

标签: jquery datatables average

Talktime
01:02:55
00:53:59
01:16:59
01:13:30

对于上面的例子,我们如何在JQUERY DATATABLES中应用SUM和AVERAGE

    jQuery.fn.dataTable.Api.register( 'sumTime()', function ( ) {
return this.flatten().reduce( function ( a, b ) {

    var timeRegexp = /^(\d{2}):(\d{2}):(\d{2})$/

    var matches = a.match(timeRegexp);
    if (matches) {
        var hh = matches[1], mm = matches[2], ss = matches[3];
        var intervalAsSeconds = hh * 24 + mm * 60 + ss;
        return b + intervalAsSeconds;
    }

    return a + b;
}, 0 );
} );

以上代码适用于SUM并且无法正常工作

1 个答案:

答案 0 :(得分:0)

以下为我工作

时间的总和:

jQuery.fn.dataTable.Api.register( 'sumTime()', function () {
  var data = this.flatten();
  var pageTotal_Duration = data.reduce( function ( a, b ) {
       return moment.duration(a).asMilliseconds() + moment.duration(b).asMilliseconds()
  }, 0);

   return moment.utc(pageTotal_Duration).format("HH:mm:ss");
});

平均时间:

jQuery.fn.dataTable.Api.register( 'averageTime()', function () {
   var data = this.flatten();
   var pageTotal_DurationA = data.reduce( function ( a, b ) {
       return moment.duration(a).asMilliseconds() + moment.duration(b).asMilliseconds()
   }, 0);

   return moment.utc(pageTotal_DurationA/data.length).format("HH:mm:ss");
});