2013-01-04 10 views
8

Ho un contatore 'tempo rimanente' in atto per i caricamenti di file. La durata residua viene calcolata e convertita in millisecondi modo:Come posso umanizzare questa durata completa in moment.js/javascript

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

Poi costruire un array che intendo costruire in una stringa umanizzato. La matrice è la seguente:

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

Tutto questo funziona perfettamente e se l'uscita ho una stringa che rappresenta questi dati in questo modo:

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

mi restituisce un bel semplice flusso di tempo rimanente in questo modo:

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

Quello che ora devo fare è umanizzare questo output in modo che la stringa sia costruita in base al tempo rimanente. es

  • 2 anni e 3 mesi restanti
  • 1 ore, 32 minuti e 41 secondi rimanenti
  • 7 secondi rimanenti
  • 3 minuti 46 secondi rimanenti
  • 6 secondi rimanenti

ecc ... ecc ...

Ora so che ment.js ha l'abilità di umanizzare automaticamente le durate che funziona bene per valori singoli ma questo può avere più valori possibili (ore/minuti/secondi ecc.)

Come posso andare a umanizzare questi dati sia con moment.js che con costruendo manualmente la stringa?

Grazie in anticipo.

risposta

7

penso che la cosa migliore sarebbe qualcosa di simile:

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

In alternativa, è possibile utilizzare questa funzione:

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

Esso restituisce "x <time> and y <time> remaining", per i 2 valori più grandi. (o solo secondi nel ultimo caso

+0

Grazie Cerberus, ho pensato di fare qualcosa di simile (anche se in un ciclo attraverso l'array), ma se la durata è grande per esempio 2 anni vorrei solo restituire gli anni, ma per le durate più piccole vorrei tornare diversi (3 minuti e 42 secondi). Speravo moment.js già gestito questo, ma non credo. – gordyr

+0

Perfetto! Enorme grazie per il tuo aiuto! – gordyr

+0

@gordyr: Nessun problema: D – Cerbrus

9

Biblioteca HumanizeDuration.js suona come esattamente quello che vuoi.

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

Sembra che la mia risposta è un po 'tardi, ma forse ti aiuto gli altri a guardare a questa domanda!

+0

Questa è una biblioteca fantastica che hai lì! Ottimo lavoro! – raidenace

+0

+ 100 Grazie mille – DogCoffee

2

Si dovrebbe dare una prova su questo plugin: moment-duration-format

La sua sintassi è molto conveniente:

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" 
Problemi correlati