2017-10-05 161 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; 
    }; 
    }); 
}