2017-10-05 159 views
0

我有开始日期,时间和结束日期,时间以及我正在查找的总旅行时间。输出以毫秒为单位,我需要将其转换为小时格式。通过在这里搜索其他答案,我尝试了以下但没有结果。将毫秒转换为小时格式

<md-cell *mdCellDef="let row"> {{row?.duration | formatDuration}} </md-cell> 

和TS文件:

export class StoppageComponent implements OnInit { 

    constructor() { 
    } 

    ngOnInit() { 
    } 


     filter('formatDuration', function() { 
     return function (input) { 
      var totalHours, totalMinutes, totalSeconds, hours, minutes, seconds, result=''; 

      totalSeconds = input/1000; 
      totalMinutes = totalSeconds/60; 
      totalHours = totalMinutes/60; 

      seconds = Math.floor(totalSeconds) % 60; 
      minutes = Math.floor(totalMinutes) % 60; 
      hours = Math.floor(totalHours) % 60; 

      if (hours !== 0) { 
       result += hours+':'; 

       if (minutes.toString().length == 1) { 
        minutes = '0'+minutes; 
       } 
      } 

      result += minutes+':'; 

      if (seconds.toString().length == 1) { 
       seconds = '0'+seconds; 
      } 

      result += seconds; 

      return result; 
     }; 
     }); 
    } 

我认为错误是与TS文件,因为我在新的角度。

是否有任何使用管道而不使用函数的直接转换?

回答

0

其他答案看起来很复杂,终于找到了一个解决方案我自己..

HTML作为,

<md-cell *mdCellDef="let row"> {{getFormathours(row?.duration)}} </md-cell> 

和TS,

getFormathours(input) { 
    var totalHours, totalMinutes, totalSeconds, hours, minutes, seconds, result=''; 
    totalSeconds = input/1000; 
    totalMinutes = totalSeconds/60; 
    totalHours = totalMinutes/60; 

    seconds = Math.floor(totalSeconds) % 60; 
    minutes = Math.floor(totalMinutes) % 60; 
    hours = Math.floor(totalHours) % 60; 

    console.log (hours + ' : ' + minutes + ' : ' + seconds); 
    if (hours !== 0) { 
     result += hours+' hr:'; 

     if (minutes.toString().length == 1) { 
      minutes = '0'+minutes; 
     } 
    } 

    result += minutes+' min'; 

     if (seconds.toString().length == 1) { 
      seconds = '0'+seconds; 
     } 

     result += seconds; 

    return result; 
} 

这给了,我需要和更透明的溶液基于我的问题的确切输出..无论如何,我欣赏别人的答案太为你的努力。

0

你不需要重新发明轮子。您可以使用Date对象获取持续时间。

假设你有2个约会对象,

  • 获取它们之间的区别。既然你已经有了毫秒,你已经完成了这一步。
  • 现在创建一个新的日期对象并删除时间值。
  • 现在,当您为其设置新时间值(差异)时,您具有所有值。只需使用该函数并获取值并以您的格式显示即可。

function getTravelDuration(date1, date2) { 
 
    var diff = +date2 - +date1; 
 
    var today = new Date(); 
 
    today.setHours(0,0,0,0); 
 
    var arrival = new Date(+today + diff); 
 
    var duration = ['getFullYear', 'getMonth', 'getDate', 'getHours', 'getMinutes', 'getSeconds'].reduce(function(p,c,i,a){ 
 
    var value = arrival[c]() - today[c](); 
 
    if(value) { 
 
     p += value; 
 
     p += ((c === 'getFullYear') ? ' Year' : c.replace('get', ' ')) + ' ' 
 
    } 
 
    return p; 
 
    }, ''); 
 
    console.log(duration); 
 
} 
 

 
function doubleDigits(str){ 
 
    return ('00' + str).slice(-2) 
 
} 
 

 
getTravelDuration(new Date(2017, 10, 5, 5), new Date(2017, 10, 5, 15, 23)); 
 
getTravelDuration(new Date(2017, 10, 5, 5), new Date(2017, 10, 6, 15, 23, 30));

0

对我来说,最好的选择(没有错误)是使用时刻库​​,而不是日期。 请检查https://momentjs.com/docs/

var t1 = moment('2017/10/5 15:23'); 
var t2 = moment('2017/10/5 15:23'); 

var differenceMinutes = t1.diff(t2, 'minutes'); 
var differenceMilliseconds = t1.diff(t2, 'minutes'); 

使用流行和测试库,可能会更好。您可以在角模板中使用:

https://github.com/urish/angular2-moment

例子:

{{startDate | amDifference: endDate :'' }} 
0

您可以使用此代码直接

export class StoppageComponent implements OnInit { 

constructor() { 
} 

ngOnInit() { 
} 


filter('formatDuration', function() { 
    return function (input) { 
     var result = new Date(input); 
var n = (d.getDate().toString()) +'/'+ (d.getMonth().toString())+'/' +(d.getFullYear().toString()) + ' '+ (d.getHours().toString()) +':'+ (d.getHours().toString()) +':'+ (d.getSeconds().toString()); 

     return result; 
    }; 
    }); 
}