I have a time counter left to upload files. The remaining duration is calculated and converted to milliseconds as follows:
var elapsedTime = e.timeStamp - timestarted; var speed = e.loaded / elapsedTime; var estimatedTotalTime = e.totalSize / speed; var timeLeftInSeconds = (estimatedTotalTime - elapsedTime) / 1000;
Then I create an array, which I am going to build in 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()) };
All this works fine, and if I output a string representation of this data like this:
console.log(time.years + ' years, ' + time.months + ' months, ' + time.days + ' days, ' + time.hours + ' hours, '+ time.minutes + ' minutes, ' + time.seconds + ' seconds');
I return a nice simple stream of remaining time:
0 years, 0 months, 0 days, 0 hours, 1 minutes, 7 seconds
What I now need to do is to humanize this conclusion so that the line is constructed depending on the remaining time. eg
- 2 years and 3 months left
- 1 hour, 32 minutes and 41 seconds remaining
- 7 seconds left
- 3 minutes remaining: 46 seconds remaining
- 6 seconds left
etc ... etc ...
Now I know that moment.js has the ability to automatically characterize a duration that works fine for single values, but it can have several possible values ββ(hours / minutes / seconds, etc.)
How can I characterize this data using either moment.js or manually creating a string?
Thanks in advance.