2013-01-04 89 views
8

這個完整的持續時間人性化我有一個'剩餘時間'計數器的文件上傳。剩餘持續時間被計算並轉化爲毫秒,像這樣:我怎樣才能在moment.js/javascript

var elapsedTime = e.timeStamp - timestarted; 
var speed = e.loaded/elapsedTime; 
var estimatedTotalTime = e.totalSize/speed; 
var timeLeftInSeconds = (estimatedTotalTime - elapsedTime)/1000; 

我然後建立我打算建立成一個人源化的字符串的數組。數組如下:

var time = { 
       years : Math.round(moment.duration(timeLeftInSeconds, 'milliseconds').years()), 
       months : Math.round(moment.duration(timeLeftInSeconds, 'milliseconds').months()), 
       days : Math.round(moment.duration(timeLeftInSeconds, 'milliseconds').days()), 
       hours : Math.round(moment.duration(timeLeftInSeconds, 'milliseconds').hours()), 
       minutes : Math.round(moment.duration(timeLeftInSeconds, 'milliseconds').minutes()), 
       seconds : Math.round(moment.duration(timeLeftInSeconds, 'milliseconds').seconds()) 
}; 

這一切工作完美,如果我輸出這個數據的字符串表示,像這樣:

console.log(time.years + ' years, ' + time.months + ' months, ' + time.days + ' days, ' + time.hours + ' hours, '+ time.minutes + ' minutes, ' + time.seconds + ' seconds'); 

我它返回的剩餘時間一個不錯的簡單流,像這樣:

0 years, 0 months, 0 days, 0 hours, 1 minutes, 7 seconds 

我現在需要做的是人性化這個輸出,以便字符串的建立取決於剩下的時間。例如

  • 2年,其餘
  • 1小時32分41秒的剩餘
  • 7秒剩餘
  • 3分鐘46秒剩餘
  • 6秒剩餘3個月

等...等...

現在我知道,莫ment.js具有自動人性化持續時間的能力,對於單個值可以很好地工作,但是這可以具有多個可能的值(小時/分鐘/秒等)

我該如何去使用moment.js或通過手動建立字符串?

在此先感謝。

回答

7

我覺得你最好的選擇將是這樣的:

function humanize(time){ 
    if(time.years > 0){ return time.years + ' years and '  + time.months + ' months remaining';} 
    if(time.months > 0){ return time.months + ' months and ' + time.days  + ' days remaining';} 
    if(time.days > 0){ return time.days + ' days and '  + time.hours + ' hours remaining';} 
    if(time.hours > 0){ return time.hours + ' hours and '  + time.minutes + ' minutes and ' + time.seconds + ' seconds remaining';} 
    if(time.minutes > 0){ return time.minutes + ' minutes and ' + time.seconds + ' seconds remaining';} 
    if(time.seconds > 0){ return time.seconds + ' seconds remaining';} 
    return "Time's up!"; 
} 

或者,您可以使用此功能:

function humanize(time){ 
    var o = ''; 
    for(key in time){ 
     if(time[key] > 0){ 
      if(o === ''){ 
       o += time[key] + ' ' + key + ' '; 
      }else{ 
       return o + 'and ' + time[key] + ' ' + key + ' remaining'; 
      } 
     } 
    } 
    return o + 'remaining'; 
} 

它返回"x <time> and y <time> remaining",爲2個最大的價值。 (或者只有幾秒鐘後一種情況

+0

感謝地獄犬,我想(通過陣列中的循環,雖然)做類似的事情,但如果持續時間很長,例如2年,我只想返回這些年份,但在較短的時間內,我想返回幾個(3分42秒)。我希望moment.js已經處理了這個,但我想不是。 – gordyr

+0

完美!非常感謝您的幫助! – gordyr

+0

@gordyr:完全沒問題:D – Cerbrus

9

HumanizeDuration.js庫聽起來像你想要什麼:

humanizeDuration(1);   // "1 millisecond" 
humanizeDuration(3000);  // "3 seconds" 
humanizeDuration(2012);  // "2 seconds, 12 milliseconds" 
humanizeDuration(97320000); // "1 day, 3 hours, 2 minutes" 

看起來像我的回答有點晚了,但也許它會幫助其他人在看這個問題!

+0

這是一個很棒的圖書館!做得好! – raidenace

+0

+ 100非常感謝 – DogCoffee

2

你應該給這個插件一試:moment-duration-format

它的語法是非常方便的:

var moment = require('moment'); 
require("moment-duration-format"); 
moment.duration(32832, "seconds").format("h [hrs]: m [min]: s [sec]") 
// => 9 hrs: 7 min: 12 sec"