Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 807
gordyr Avatar asked Jan 04 '13 12:01

gordyr


3 Answers

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!

like image 103
Evan Hahn Avatar answered Nov 10 '22 17:11

Evan Hahn


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';
}

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

like image 10
Cerbrus Avatar answered Nov 10 '22 17:11

Cerbrus


You should give a try on this plugin: moment-duration-format

Its syntax is very convenient:

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" 
like image 4
Hoang Le Avatar answered Nov 10 '22 18:11

Hoang Le