【问题标题】:Moment.js : change the default humanize() behaviorMoment.js :更改默认的 humanize() 行为
【发布时间】:2015-08-04 08:39:36
【问题描述】:

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”这个词,因为它将用于许多不同的语言。

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

【问题讨论】:

    标签: javascript momentjs


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-07-24
        • 2016-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-14
        • 1970-01-01
        • 2012-06-30
        相关资源
        最近更新 更多