2012-07-02 7 views
29

Probé este en moment.jsduraciones humanizados completas en moment.js

moment.duration(375,'days').humanize() 

y obtener "al año" como respuesta, pero yo esperaría "un año y 10 días". ¿Hay alguna manera en moment.js de obtener el valor humanizado completo?

Respuesta

7

que estaba buscando al mismo tema y parece que no hay un plan en el apoyo a esto en el futuro ...

Aunque una solución propuesta es hacer una definición de lenguaje que anula implementación predeterminada de mensajes humanizados :

https://github.com/timrwood/moment/issues/348

una especie de exageración si me preguntas ...

11

Moment.js está proporcionando la función fromNow para obtener duraciones de tiempo en lectura humana fromat, consulte http://momentjs.com/docs/#/displaying/fromnow/

Ejemplo:

moment([2007, 0, 29]).fromNow(); // 4 years ago 
moment().subtract(375, 'days').fromNow(); // a year ago 

Es necesario utilizar lib tercer partido como lo sugiere @Fluffy

+1

No hay necesidad de una tercera parte. Puede usar ['moment.relativeTimeThreshold ('y', 365)'] (https://github.com/moment/momentjs.com/blob/master/docs/moment/07-customization/13-relative-time- threshold.md) para establecer el redondeo. – RJFalconer

0

Ésta es mi solución en CoffeeScript:

humanizeDuration = (eventDuration)-> 
    eventMDuration = Moment.duration(eventDuration, 'seconds'); 
    eventDurationString = "" 
    if (eventMDuration.days() > 0) 
     eventDurationString += " " + Moment.duration(eventMDuration.days(), 'days').humanize() 
    if (eventMDuration.hours() > 0) 
     eventDurationString += " " + Moment.duration(eventMDuration.hours(), 'hours').humanize() 
    if (eventMDuration.minutes() > 0) 
     eventDurationString += " " + Moment.duration(eventMDuration.minutes(), 'minutes').humanize() 

    eventDurationString.trim() 
1

He escrito este código javascript para humanizar la duración,

function humanizeDuration(timeInMillisecond) { 
    var result = ""; 
    if (timeInMillisecond) { 
     if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60 * 24 * 30 * 12))) > 0) {//year 
      result = result === 1 ? result + " Year" : result + " Years"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60 * 24 * 30))) > 0) {//months 
      result = result === 1 ? result + " Month" : result + " Months"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60 * 24))) > 0) {//days 
      result = result === 1 ? result + " Day" : result + " Days"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60 * 60))) > 0) {//Hours 
      result = result === 1 ? result + " Hours" : result + " Hours"; 
     } else if ((result = Math.round(timeInMillisecond/(1000 * 60))) > 0) {//minute 
      result = result === 1 ? result + " Minute" : result + " Minutes"; 
     } else if ((result = Math.round(timeInMillisecond/1000)) > 0) {//second 
      result = result === 1 ? result + " Second" : result + " Seconds"; 
     } else { 
      result = timeInMillisecond + " Millisec"; 
     } 
    } 
    return result; 
} 
3

Esta es mi solución, me gusta mejor que los otros aquí:

val moment1 = moment(); 
val moment2 = mement(); 
console.log(moment.duration(moment1.diff(moment2)).humanize()); 
2

Usar moment.relativeTimeThreshold('y', 365) para configurar el redondeo.

moment.relativeTimeThreshold('s', 60); 
moment.relativeTimeThreshold('m', 60); 
moment.relativeTimeThreshold('h', 24); 
moment.relativeTimeThreshold('d', 31); 
moment.relativeTimeThreshold('M', 12); 
moment.relativeTimeThreshold('y', 365); 
Cuestiones relacionadas