Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js : change the default humanize() behavior

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?

like image 652
smnbbrv Avatar asked Aug 04 '15 08:08

smnbbrv


1 Answers

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.

like image 121
Are Avatar answered Sep 29 '22 05:09

Are