这个完整的持续时间人性化我有一个'剩余时间'计数器的文件上传。剩余持续时间被计算并转化为毫秒,像这样:我怎样才能在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