這個完整的持續時間人性化我有一個'剩餘時間'計數器的文件上傳。剩餘持續時間被計算並轉化爲毫秒,像這樣:我怎樣才能在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或通過手動建立字符串?
在此先感謝。
感謝地獄犬,我想(通過陣列中的循環,雖然)做類似的事情,但如果持續時間很長,例如2年,我只想返回這些年份,但在較短的時間內,我想返回幾個(3分42秒)。我希望moment.js已經處理了這個,但我想不是。 – gordyr
完美!非常感謝您的幫助! – gordyr
@gordyr:完全沒問題:D – Cerbrus