Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

momentjs - How can I humanize this complete duration in 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 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...

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)

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

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My HumanizeDuration.js library sounds like exactly what you want:

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

Looks like my answer's a bit late, but maybe it'll help others looking at this question!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...