Skip to content Skip to sidebar Skip to footer

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.d

Solution 1:

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.

Solution 2:

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)

Post a Comment for "Moment.js : Change The Default Humanize() Behavior"