Search code examples
javascriptmomentjscountdown

How to Rename preciseDiff Key in moment-timezone?


How to Rename key of preciseDiff Function in moment-timezone?

code:

var duration = momentz.preciseDiff(0, 1528791845);

output:

1 year 8 hours 17 minutes 6 seconds

I will want to translate year, month and ... to another language.

year       año  
month      mes  
day        día  
hour       hora  
minute     minuto  
second     segundo  

Solution

  • I would imagine you would have to replace it manually. Maybe another function?

    const es = {
        year: 'año',
        years: 'años',
        month: 'mes',  
        day: 'día',
        hour: 'hora',
        hours: 'horas',
        minute: 'minuto',
        minutes: 'minutos',
        second: 'segundo',
        seconds: 'segundos'
    };
    const translateWords = (dictionary, string) => string
        .split(' ')
        .map(word=>dictionary[word]||word)
        .join(' ');
    const esString = translateWords(es, "1 year 8 hours 17 minutes 6 seconds");
    console.log(esString); // 1 año 8 horas 17 minutos 6 segundos