如何在moment.js / javascript中将这个完整的持续时间人性化 [英] How can I humanize this complete duration in moment.js / javascript

查看:185
本文介绍了如何在moment.js / javascript中将这个完整的持续时间人性化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个'剩余时间'计数器用于文件上传。计算剩余的持续时间并将其转换为毫秒,如下所示:

I have a 'time remaining' counter in place for file uploads. The remaining duration is calculated and converted into milliseconds like so:

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

然后构建一个我打算构建成人性化字符串的数组。数组如下:

I then build an array which I intend to build into a humanized string. The array is as follows:

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())
};

这一切都很完美,如果我输出这样的数据的字符串表示如下:

This all works perfectly and if I output a string representation of this data like so:

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

我返回一个很简单的剩余时间流,如下所示:

I it returns a nice simple stream of remaining time like so:

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

我现在需要做的是将此输出人性化,以便根据剩余时间构建字符串。例如

What I now need to do is humanize this output so that the string is built dependent on the time remaining. e.g


  • 剩余2年零3个月

  • 剩余1小时32分41秒

  • 剩余7秒

  • 剩余3分46秒

  • 剩余6秒

  • 2 years and 3 months remaining
  • 1 hour, 32 minutes and 41 seconds remaining
  • 7 seconds remaining
  • 3 minutes 46 seconds remaining
  • 6 seconds remaining

等......等等......

etc...etc...

现在我知道那个时刻.js有能力自动人性化持续时间适用于单个值,但这可能有多个可能的值(小时/分钟/秒等)

Now I know that moment.js has the abiility to automatically humanize durations which works fine for single values but this can have multiple possible values ( hours/minutes/seconds etc)

我怎样才能将这些数据人性化.js或通过手动构建字符串?

How can I go about humanizing this data either with moment.js or by manually building the string?

提前致谢。

推荐答案

我认为你最好的选择是这样的:

I think your best bet would be something like this:

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!";
}

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

Alternatively, you could use this function:

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>和y对于2个最大值,< time>剩余。 (或者在最后一种情况下只有几秒钟。

It returns "x <time> and y <time> remaining", for the 2 greatest values. (Or only seconds in the last case.

这篇关于如何在moment.js / javascript中将这个完整的持续时间人性化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆