Moment.js:更改默认的humanize()行为

时间:2015-08-04 08:39:36

标签: javascript momentjs

In the moment.js documentation给出了以下示例:

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

我需要接听此电话

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

输出“1分钟”,我想拥有此字符串

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

输出“24小时”。

我不能只用1替换“a”字,因为它将用于许多不同的语言。

这可能吗?也许一些简单的配置/解决方法/未记录的功能?

2 个答案:

答案 0 :(得分:4)

moment.js documentation您可以自定义它:

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"
    }
});

我知道这个解决方案并不完美,但找不到更好的。

答案 1 :(得分:0)

要从moment.duration(24, "hours").humanize();输出带有时刻的“ 24小时”,您可以像moment.relativeTimeThreshold('h',25)这样使用relative time threshold