2013-01-04 47 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"