2016-08-30 244 views
0

我有,我想變成天毫秒的時間,以分鐘爲單位小時,讓輸出看起來是這樣的:如何使用jQuery將毫秒轉換爲幾天,幾小時和幾分鐘?

「473天17小時28分鐘」

我可以」找到如何做到這一點的答案。我真的很感謝一些幫助。

+0

無關的jQuery。這只是基本的數學... –

+0

[JavaScript:將毫秒轉換爲天數,小時,分鐘和秒數的對象](https://gist.github.com/remino/1563878) – empiric

+3

可能重複[Javascript以毫秒顯示毫秒:小時:分鐘無秒](http://stackoverflow.com/questions/8528382/javascript-show-milliseconds-as-dayshoursmins-without-seconds) – depperm

回答

0

請檢查下面的代碼段。通過毫秒後,您將在天數小時和分鐘內找到結果。

function dhm(t){ 
 
    var cd = 24 * 60 * 60 * 1000, 
 
     ch = 60 * 60 * 1000, 
 
     d = Math.floor(t/cd), 
 
     h = Math.floor((t - d * cd)/ch), 
 
     m = Math.round((t - d * cd - h * ch)/60000), 
 
     pad = function(n){ return n < 10 ? '0' + n : n; }; 
 
    if(m === 60){ 
 
    h++; 
 
    m = 0; 
 
    } 
 
    if(h === 24){ 
 
    d++; 
 
    h = 0; 
 
    } 
 
    return d +" days : "+ pad(h) +" hours : "+ pad(m) + " mins "; 
 
} 
 
var days = (473 * 24 * 60 * 60 * 1000); 
 
var hours = (17 * 60 * 60 * 1000); 
 
var mins = (28 * 60 * 1000); 
 
var milliseconds = days + hours + mins; 
 
console.log(dhm(milliseconds));

0
var milliseconds = 1000000000000; 
var dateStr = new Date(milliseconds); 
var humanreadableStr = dateStr.getDay() +'Days '+dateStr.getHours() +'Hours '+dateStr.getMinutes() +'Minutes '+dateStr.getSeconds() +'Seconds'; 
相關問題