7

In the moment.js documentation the following example is given:

moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize();  // a day

I need to have for this call

moment.duration(1, "minutes").humanize();

an output "1 minute" and I want to have for this string

moment.duration(24, "hours").humanize();

an output "24 hours".

I cannot just replace the "a" word with 1 because it is going to be used for many different languages.

Is that possible? Maybe some simple config / workaround / non-documented feature?

smnbbrv
  • 23,502
  • 9
  • 78
  • 109

2 Answers2

10

From the moment.js documentation you can customize it like so:

moment.locale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "seconds",
        m:  "a minute",  //change that to "%d minute"
        mm: "%d minutes",
        h:  "an hour",   //here too
        hh: "%d hours",
        d:  "a day",     //and here
        dd: "%d days",
        M:  "a month",   //and so on...
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

I know this solution is not perfect, but couldn't find any better.

Are
  • 2,160
  • 1
  • 21
  • 31
0

To have an output "24 hours" from moment.duration(24, "hours").humanize(); with moment you can use relative time threshold like moment.relativeTimeThreshold('h',25)

Matthieu Gabin
  • 830
  • 9
  • 26